How to set zoom level of Google map according to marker location

I placed two markers on a Google map. One is in the USA, and the other is somewhere in Asia. Google only provides zoom levels of 1, 2, 3, 4, 5, 6, etc.

I do not want to hard code the zoom level. How can I display all markers based on distance? That is, if two elements are very close to each other or in the same city, a close zoom level should be used, and if they are far from each other, then the maximum zoom level should be used.

+3
source share
3 answers

, LatLngBounds , :

private void addMarkers() {
    MarkerOptions markerOptions = new MarkerOptions();
    LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();

    //add the marker locations that you'd like to display on the map
    boundsBuilder.include(new LatLng(lat1, lng1));
    boundsBuilder.include(new LatLng(lat2, lng2));

    final LatLngBounds bounds = boundsBuilder.build();

    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
    map.animateCamera(cameraUpdate);
}
+1

? API GMap2 8 , , GDirections (map {{div}}); , , div, .

0

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37,
            -121), 6)); 

https://developers.google.com/maps/documentation/android-api/views

0
source

Source: https://habr.com/ru/post/1777839/


All Articles