在谷歌地图android api v2中设置最大缩放级别

use*_*820 36 maps android zoom

我目前正在使用谷歌地图android API v2开发应用程序.我的代码如下.假设地图有几个标记并放大以显示所有标记.

LatLngBuilder.Builder builder = LatLngBounds.builder();
for(Marker m : markers){
    builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10);
Run Code Online (Sandbox Code Playgroud)

此代码工作正常,但我想在缩放级别达到17.0f时停止动画; 看来map API没有这样的方法来控制缩放级别.有人知道解决这个问题的任何想法吗?

Dmi*_*rov 27

最新的Google Maps API 更新介绍了您需要的功能:

GoogleMap.setMaxZoomPreference()

GoogleMap.setMinZoomPreference()
Run Code Online (Sandbox Code Playgroud)

但它仍然不能阻止动画播放.


Tis*_*sys 25

我在Google Maps API中找不到任何直接的解决方案.此问题的一个潜在解决方案是监听OnCameraChange事件:每当此事件触发且缩放级别高于最大缩放级别时,就可以调用animateCamera().结果代码如下:

@Override
public void onCameraChange(CameraPosition position) {
    float maxZoom = 17.0f;
    if (position.zoom > maxZoom)
        map_.animateCamera(CameraUpdateFactory.zoomTo(maxZoom));
}
Run Code Online (Sandbox Code Playgroud)

  • 现在有一个直接的解决方案,使用`GoogleMap.setMaxZoomPreference()`和`GoogleMap.setMinZoomPreference()` (11认同)
  • 谢谢你的通讯.我试过这个,但似乎onCameraChange在动画后调用,所以缩放级别有时可能比17.0更大. (3认同)

Ern*_*nie 13

以下是Arvis解决方案的相应Java代码,对我来说效果很好:

private LatLngBounds adjustBoundsForMaxZoomLevel(LatLngBounds bounds) {
  LatLng sw = bounds.southwest;
  LatLng ne = bounds.northeast;
  double deltaLat = Math.abs(sw.latitude - ne.latitude);
  double deltaLon = Math.abs(sw.longitude - ne.longitude);

  final double zoomN = 0.005; // minimum zoom coefficient
  if (deltaLat < zoomN) {
     sw = new LatLng(sw.latitude - (zoomN - deltaLat / 2), sw.longitude);
     ne = new LatLng(ne.latitude + (zoomN - deltaLat / 2), ne.longitude);
     bounds = new LatLngBounds(sw, ne);
  }
  else if (deltaLon < zoomN) {
     sw = new LatLng(sw.latitude, sw.longitude - (zoomN - deltaLon / 2));
     ne = new LatLng(ne.latitude, ne.longitude + (zoomN - deltaLon / 2));
     bounds = new LatLngBounds(sw, ne);
  }

  return bounds;
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*250 13

来自Android doc:https: //developers.google.com/maps/documentation/android-api/views

You may find it useful to set a prefered minimum and/or maximum zoom level. For example, this is useful to control the user's experience if your app shows a defined area around a point of interest, or if you're using a custom tile overlay with a limited set of zoom levels.

    private GoogleMap mMap;
    // Set a preference for minimum and maximum zoom.
    mMap.setMinZoomPreference(6.0f);
    mMap.setMaxZoomPreference(14.0f);
Run Code Online (Sandbox Code Playgroud)


Jen*_*ens 7

与Arvis解决方案相同,但使用Location's [ distanceBetween()](http://developer.android.com/reference/android/location/Location.html#distanceBetween (double, plex,double,double,float []))方法来计算距离之间点:

@Override
public void zoomToMarkers(Set<Marker> markers) {

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (Marker marker : markers) {
        builder.include(marker.getPosition());
    }
    LatLngBounds bounds = builder.build();

    // Calculate distance between northeast and southwest
    float[] results = new float[1];
    android.location.Location.distanceBetween(bounds.northeast.latitude, bounds.northeast.longitude,
            bounds.southwest.latitude, bounds.southwest.longitude, results);

    CameraUpdate cu = null;
    if (results[0] < 1000) { // distance is less than 1 km -> set to zoom level 15
        cu = CameraUpdateFactory.newLatLngZoom(bounds.getCenter(), 15);
    } else {
        int padding = 50; // offset from edges of the map in pixels
        cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    }
    if (cu != null) {
        mMap.moveCamera(cu);
    }
}
Run Code Online (Sandbox Code Playgroud)


Lin*_*nga -4

该地图有一个名为 maxZoom 的属性。只需在创建地图时将其设置为您的值即可