Setting max zoom level in google maps android api v2

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

    Same as Arvis solution but using Location's [distanceBetween()](http://developer.android.com/reference/android/location/Location.html#distanceBetween(double, double, double, double, float[])) method to calculate distance between to points:

    @Override
    public void zoomToMarkers(Set 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);
        }
    }
    

提交回复
热议问题