【问题标题】:Calculate Zoom Level for Google Map for two LatLong values为两个 LatLong 值计算 Google Map 的缩放级别
【发布时间】:2013-09-10 20:29:13
【问题描述】:

我在SherlockFragmentActivity 中使用com.google.android.gms.maps.GoogleMap

XML 代码是这样的:

            <fragment
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="fill_parent"
                android:layout_height="150dip" />

int zoomLevel = ? // 如何计算两个不同 latlong 值的缩放级别 由于 android map v3 需要将缩放级别告知为 int

map.setZoom(zoomLevel);

我的起始值和目标值为com.google.android.gms.maps.model.LatLng

LatLng 开始,结束;

我正在添加一个像GoogleLocation.addPolyLineOnGMap(mMap, startPoint, endPoint, startMarker, endMarker)这样的插件

我的问题是如何计算 Google 地图的缩放级别,以便它可以在地图上适当地显示两个标记。

【问题讨论】:

  • Google Maps for Android 到目前为止有 V2,而不是 V3(V3 用于 JavaScript)。 “计算缩放级别”是什么意思?
  • 请使用正确的英语并具体说明问题
  • 从代码 (GeoPoint) 来看,它看起来像 Google Maps API V1。

标签: android google-maps


【解决方案1】:

使用 LatLngBounds.Builder 在其中添加所有边界并构建它,然后创建 CameraUpdate 对象并将边界传递给它 updatefactory 和填充。使用此 CameraUpdate 对象为地图相机设置动画。

LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (Marker m : markers) {
            builder.include(m.getPosition());
        }
        LatLngBounds bounds = builder.build();
        int padding = ((width * 10) / 100); // offset from edges of the map
                                            // in pixels
        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
                padding);
        mMap.animateCamera(cu);

【讨论】:

    【解决方案2】:

    对我来说,我需要通过GoogleMapOptions 计算初始地图设置的缩放比例,因此使用LatLngBounds.Builder 不会工作,也没有优化。这就是我根据城市的东北和西南坐标计算缩放的方式

    它引用了herethis answer,你可以简单地将下面的代码放到你的助手类中:

    final static int GLOBE_WIDTH = 256; // a constant in Google's map projection
    final static int ZOOM_MAX = 21;
    
    public static int getBoundsZoomLevel(LatLng northeast,LatLng southwest,
                                         int width, int height) {
        double latFraction = (latRad(northeast.latitude) - latRad(southwest.latitude)) / Math.PI;
        double lngDiff = northeast.longitude - southwest.longitude;
        double lngFraction = ((lngDiff < 0) ? (lngDiff + 360) : lngDiff) / 360;
        double latZoom = zoom(height, GLOBE_WIDTH, latFraction);
        double lngZoom = zoom(width, GLOBE_WIDTH, lngFraction);
        double zoom = Math.min(Math.min(latZoom, lngZoom),ZOOM_MAX);
        return (int)(zoom);
    }
    private static double latRad(double lat) {
        double sin = Math.sin(lat * Math.PI / 180);
        double radX2 = Math.log((1 + sin) / (1 - sin)) / 2;
        return Math.max(Math.min(radX2, Math.PI), -Math.PI) / 2;
    }
    private static double zoom(double mapPx, double worldPx, double fraction) {
        final double LN2 = .693147180559945309417;
        return (Math.log(mapPx / worldPx / fraction) / LN2);
    }
    

    简单地由new LatLng(lat-double, lng-double)创建LatLng

    widthheight 是以像素为单位的地图布局大小

    【讨论】:

    • 注意:widthheight 需要以密度像素而不是普通像素来衡量,否则在具有更高密度屏幕的新设备上缩放级别将不正确。
    【解决方案3】:

    在安卓中:

    LatLngBounds group = new LatLngBounds.Builder()
                    .include(tokio)   // LatLgn object1
                    .include(sydney)  // LatLgn object2
                    .build();
    
    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(group, 100)); // Set Padding and that's all!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 2011-08-28
      • 2011-07-30
      相关资源
      最近更新 更多