Setting max zoom level in google maps android api v2

后端 未结 16 2129
无人共我
无人共我 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:44

    I have not found any direct solution in the Google Maps API. A potential solution to this problem consists in listening against the OnCameraChange event: Whenever this event triggers and the zoom level is above the maximum zoom level, it is possible to call animateCamera(). The resulting code would be the following:

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

    Before animate Camera you can check if SW and NE points of bounds are not too close and if necessary adjust bounds:

            ...
            LatLngBounds bounds = builder.Build();
    
            var sw = bounds.Southwest;
            var ne = bounds.Northeast;
            var deltaLat = Math.Abs(sw.Latitude - ne.Latitude);
            var deltaLon = Math.Abs(sw.Longitude - ne.Longitude);
    
            const double zoomN = 0.005; // set whatever zoom coefficient you need!!!
            if (deltaLat < zoomN) {
                sw.Latitude = sw.Latitude - (zoomN - deltaLat / 2);
                ne.Latitude = ne.Latitude + (zoomN - deltaLat / 2);
                bounds = new LatLngBounds(sw, ne);
            }
            else if (deltaLon < zoomN) {
                sw.Longitude = sw.Longitude - (zoomN - deltaLon / 2);
                ne.Longitude = ne.Longitude + (zoomN - deltaLon / 2);
                bounds = new LatLngBounds(sw, ne);
            }
    
            map.animateCamera(CameraUpdateFactory.NewLatLngBounds(bounds, 10);
    

    ps. My example is in c# for Xamarin but you can easly adjust it for java

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

    This is from the API reference for the include(position) you're using:

    "Includes this point for building of the bounds. The bounds will be extended in a minimum way to include this point. More precisely, it will consider extending the bounds both in the eastward and westward directions (one of which may wrap around the world) and choose the smaller of the two. In the case that both directions result in a LatLngBounds of the same size, this will extend it in the eastward direction."

    The map will zoom out until it can show all of the markers you're adding in your for loop.

    If you want to only zoom out to 17 and still show markers, animate to zoom level 17 first, then get the bounds for it, and then add your markers.

    @Override
    public void onCameraChange(CameraPosition camPos) {
    
        if (camPos.zoom < 17 && mCurrentLoc != null) {
            // set zoom 17 and disable zoom gestures so map can't be zoomed out
            // all the way
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position,17));
            mMap.getUiSettings().setZoomGesturesEnabled(false);
        }
        if (camPos.zoom >= 17) {
            mMap.getUiSettings().setZoomGesturesEnabled(true);
        }
    
        LatLngBounds visibleBounds =  mMap.getProjection().getVisibleRegion().latLngBounds;
    
    //add the markers
    
    0 讨论(0)
  • 2020-12-02 17:48

    On the new Google Maps 9.8

    com.google.android.gms:play-services-maps:9.8.0

    This is how I did and worked

        googleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
            @Override
            public void onCameraMove() {
                Log.d(App.TAG, "onCameraMove");
    
                CameraPosition position = googleMap.getCameraPosition();
    
                float maxZoom = 9.0f;
                if (position.zoom > maxZoom) {
    
                    googleMap.setMinZoomPreference(maxZoom);
                }
    
                Log.d(App.TAG, "position.zoom = " + position.zoom);
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题