android Gmaps v2 : Bounds and max zoom level

2019-07-29 11:00发布

问题:

it is about android and google maps v2. I want to set max zoom level with bounds. Here is the method I'm using :

gMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding));

I've found this link which gave me a possible workaround Setting max zoom level in google maps android api v2

Here is the workaraound found

gMap.setOnCameraChangeListener(new OnCameraChangeListener() {
    @Override
    public void onCameraChange(CameraPosition position) {
        if (position.zoom > DEFAULT_ZOOM)
            gMap.animateCamera(CameraUpdateFactory.zoomTo(DEFAULT_ZOOM));
    }
});

But this solution zoom in until the zoom level defined by first animateCamera and then zoom out until DEFAULT_ZOOM if (DEFAULT_ZOOM < position.zoom). In this case, there is two animateCamera

How to avoid that ? And make only one animateCamera

Thx in advance

回答1:

Ok, for now, my only solution is related here https://stackoverflow.com/a/19343818/1646479

Here is the workaround:

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;
}

but I'm looking for a solution to translate android zoom level (16 in my case) to zoomN.