Search code examples
androidgoogle-maps-api-2

android Zoom-to-Fit All Markers on Google Map v2


How to zoom in/out the map view such a way that it cover all markers?

i am reviewing sample code of gmap V2 given in sdk. It lays on sdk\extras\google\google_play_services\samples\maps.

Thanks in advance.


Solution

  • Try using LatLngBounds :

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    builder.include(Latlng1);
    builder.include(Latlng2);
    LatLngBounds bounds = builder.build();
    map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));