Setting max zoom level in google maps android api v2

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

提交回复
热议问题