【问题标题】:Google Maps proper padding around markers谷歌地图在标记周围正确填充
【发布时间】:2015-11-30 10:54:49
【问题描述】:

我目前有以下代码:

private void updateCamera() {
    if (markerOptionses.isEmpty()) {
        return;
    }
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (LatLng position : markerOptionses.keySet()) {
        builder.include(position);
    }
    final LatLngBounds bounds = builder.build();

    MainActivity.getInstance().runOnUiThread(
            new Runnable() {
                @Override
                public void run() {
                    if (mapView.getParent() != null) {
                        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(
                                bounds,0                                  );
                        mapView.getMap().animateCamera(cu);
                    }
                }
            }                               );
}

此代码可以很好地缩放,以便所有标记位置都在视图内。然而问题是,顶部的“引脚”被切断了。它固定的点被准确显示,但标记本身不是。

为了解决这个问题,我可以在 CameraUpdate 中添加一个填充,结果类似于

CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,50);

这在较小的设备上运行良好,但在较大的设备上 50 太少了。如果我将它设置为 150,这在较大的设备上可以正常工作,但在较小的设备上就太多了。

什么是解决这个问题的好方法?有什么方法可以计算出标记的高度(以像素为单位)并将其用作填充?

【问题讨论】:

  • this link可能对你有帮助。
  • 解决方法是选择 50dp,然后将 50dp 转换为像素,在小型设备上它会很小,而在高端设备上它会很有价值。 --> 将 dp 转换为 pix 的代码:Resources r = getResources(); float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, r.getDisplayMetrics());
  • @Clairvoyant 不幸的是,填充被指定为30

标签: android google-maps


【解决方案1】:

See the Android documentation for CameraUpdateFactory#newLatLngBounds(LatLngBounds, int):

padding 在边界框边缘和视图边缘之间留出的空间(以 px 为单位)。该值应用于边界框的所有四个边。

因此您需要计算标记的大小,这将构成填充的基础:

private int calculateMapMarkerPadding() {
    BitmapDrawable bd = (BitmapDrawable) ContextCompat
            .getDrawable(this, R.drawable.map_marker);
    final int width = bd.getBitmap().getWidth();
    final int height = bd.getBitmap().getHeight();
    return (int)Math.round(Math.max(width, height) * MAP_BOUNDS_PADDING);
}

注意:乘以 MAP_BOUNDS_PADDING(例如 1.2 或 120%)而不是使用固定数量。然后更新您的地图摄像头代码:

    final int padding = calculateMapMarkerPadding();
    final CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(builder.build(), padding);
    mapView.getMap().animateCamera(cu);

【讨论】:

    猜你喜欢
    • 2014-06-29
    • 2014-03-23
    • 1970-01-01
    • 2014-02-28
    • 2018-02-11
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多