7

Is there a way to make a map zoom in as far as possible but keeping all of the markers on screen at the same time. I have six markers in a non uniform shape. I was thinking of just taking their centre and locating the map to that, but I still then do not know how far programmatically I can zoom in the map.

These markers change location every time the map is created. Ideally I would have a script that zooms the map in so all are included in the view.

So you understand. I am creating the markers one at a time from json with the following line in a loop.

mMap.addMarker(new MarkerOptions().position(new LatLng(lat,lng)).title(c.getString("title")));
Somk
  • 11,869
  • 32
  • 97
  • 143

2 Answers2

24

You should use the CameraUpdate class to do (probably) all programmatic map movements.

To do this, first calculate the bounds of all the markers like so:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for each (Marker m : markers) {
    builder.include(m.getPosition());
}

LatLngBounds bounds = builder.build();

Then obtain a movement description object by using the factory: CameraUpdateFactory:

int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);

Finally move the map:

googleMap.moveCamera(cu);

Or if you want an animation:

googleMap.animateCamera(cu);

*Got from another post: Android map v2 zoom to show all the markers

Community
  • 1
  • 1
Jai Kumar
  • 920
  • 1
  • 7
  • 14
  • 1
    Could you please link to the original post you obtained this from for some more context. – Somk Jul 12 '13 at 11:38
  • Thats the entire thing. any way here's it. http://stackoverflow.com/a/14828739/1837759 – Jai Kumar Jul 12 '13 at 11:40
  • Thank you. This line is causing me confusion though foreach (Marker m : markers) { How do I obtain the markers? – Somk Jul 12 '13 at 11:44
  • u can do without it too just use ur marker object to do it. builder.include(m.getPosition()); Here m is the marker object. – Jai Kumar Jul 12 '13 at 12:38
1

You may want to look at this:

https://developers.google.com/maps/documentation/android/views#changing_camera_position

Androiderson
  • 16,865
  • 6
  • 62
  • 72
  • This is the best just update to latest you never know when google is going to remove that entire package :p – silentsudo Oct 24 '16 at 09:49