Reputation: 3774
I am using MapView
and this map have many markers
.
How to enable the Zoom to be set automatically to how all markers in the map ?
Upvotes: 0
Views: 1217
Reputation: 33792
You have to get the 4 corners that form the bounds containing all of the markers. Then zoom to include the span while you move to the center of the area. If that makes sense. See the code below.
int minLat = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int minLon = Integer.MAX_VALUE;
int maxLon = Integer.MIN_VALUE;
for (GeoPoint item : items)
{
int lat = item.getLatitudeE6();
int lon = item.getLongitudeE6();
maxLat = Math.max(lat, maxLat);
minLat = Math.min(lat, minLat);
maxLon = Math.max(lon, maxLon);
minLon = Math.min(lon, minLon);
}
double fitFactor = 1.5;
mapController.zoomToSpan((int) (Math.abs(maxLat - minLat) * fitFactor), (int)(Math.abs(maxLon - minLon) * fitFactor));
mapController.animateTo(new GeoPoint( (maxLat + minLat)/2,
(maxLon + minLon)/2 ));
Upvotes: 5