1

I have an ArrayList, each of these LatLng is a marker on my map The list can have anywhere from 0 to 1 to n points. They may or may not be distant within each others.

Can someone help me, given a height ("h"dp) and a width ("w"dp) of the map area, determine:

  1. the "center" of this set
  2. the appropriate zoom level (z) of this set, so that all markers are visible within the frame of size "h" by "w", but a (z+1) zoom level will not show all the points.
Stephane Maarek
  • 5,202
  • 9
  • 46
  • 87

1 Answers1

1

To find the center of a set of LatLng points:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng point: pointSet) {
   builder.include( point);
}
LatLngBounds bounds = builder.build();
LatLng centerPoint = bounds.getCenter();


As for zooming, the simplest way would be to call map.animate:

mapObj.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, PADDING_VALUE));

Which is guaranteed to set the map to the greatest zoom level that contains the bounds input (plus padding).


And if you're still interested in knowing the required zoom level:

float zoomLevel = mapObj.getCameraPosition().zoom; // call after(!) animateTo
Gilad Haimov
  • 5,767
  • 2
  • 26
  • 30