Setting max zoom level in google maps android api v2

后端 未结 16 2128
无人共我
无人共我 2020-12-02 17:14

I\'m currently working on developing apps by using Google maps android API v2. My code is as follows. Suppose map has several markers and zoom up to show all markers in disp

相关标签:
16条回答
  • 2020-12-02 17:34

    Here is the corresponding Java code for Arvis' solution which worked well for me:

    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;
    }
    
    0 讨论(0)
  • 2020-12-02 17:35

    The map has a property called maxZoom. Simply set this to your value when you create your map

    0 讨论(0)
  • 2020-12-02 17:36

    @kasimir's approach sets a minimum number of degrees in either the latitude or longitude, and felt a little hard to read. So I tweaked it to just set a minimum on the latitude, which I felt like was a bit more readable:

    private LatLngBounds adjustBoundsForMinimumLatitudeDegrees(LatLngBounds bounds, double minLatitudeDegrees) {
        LatLng sw = bounds.southwest;
        LatLng ne = bounds.northeast;
        double visibleLatitudeDegrees = Math.abs(sw.latitude - ne.latitude);
    
        if (visibleLatitudeDegrees < minLatitudeDegrees) {
            LatLng center = bounds.getCenter();
            sw = new LatLng(center.latitude - (minLatitudeDegrees / 2), sw.longitude);
            ne = new LatLng(center.latitude + (minLatitudeDegrees / 2), ne.longitude);
            bounds = new LatLngBounds(sw, ne);
        }
    
        return bounds;
    }
    
    0 讨论(0)
  • 2020-12-02 17:38

    We can not restrict map zoomin feature directly, but we can try to get same feature like this.

    add map.setOnCameraChangeListener

    final float maxZoom = 10.0f;
    
    @Override
    public void onCameraChange(CameraPosition position) {
    
        if (position.zoom > maxZoom)
            map.animateCamera(CameraUpdateFactory.zoomTo(maxZoom));
    }
    
    0 讨论(0)
  • 2020-12-02 17:41

    You can get the max zoom level by calling it getMaxZoomLevel() and then set that value:

    mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(fromPosition, getMaxZoomLevel()));
    
    0 讨论(0)
  • 2020-12-02 17:43

    From 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);
    
    0 讨论(0)
提交回复
热议问题