Java Enthusiast Java Enthusiast - 2 months ago 14
Android Question

Dynamic zoom in Google Maps API v2 Android

In Google Maps API v2, the method

animateCamera
is used to define the zoom level for viewing. For example,

googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat1, lng1), 12));


Here, the zoom level is fixed to 12. How do I make sure that the zoom value fixed here, will allow the user to view source and destination points along the route without manual zooming.

I know using trial and error methods and some
if-else
conditions, the values can be determined. But is there a more sophisticated way of doing this?

Answer

You can use LatLngBounds like this:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(startPoint);
builder.include(endPoint);
LatLngBounds bound = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bound, 25), 1000, null);

startPoint and endPoint are LatLng objects.