I have a set of GeoPoints and want to find the Lat/Lon center and and spans that include all these GeoPoints (so they're visible all on the MapView).
On a planar map this would be rather simple, find the highest and lowest x/y values, find the center by adding half of the absolute distance to the lower value.
But how is this done for the spherical world map, where the max/min values of -180/+180 and +90/-90 are next to each other?
Here's what I'm trying to do:
public void zoomToGeoPoints(GeoPoint... geoPoint) {
MapController mc = getController();
if (geoPoint == null) {
Log.w(TAG, "No geopoints passed, doing nothing!");
return;
}
// Set inverse max/min start values
int maxLat = (int) (-90 * 1E6);
int maxLon = (int) (-180 * 1E6);
int minLat = (int) (90 * 1E6);
int minLon = (int) (180 * 1E6);
// Find the max/min values
for (GeoPoint gp : geoPoint) {
maxLat = Math.max(maxLat, gp.getLatitudeE6());
maxLon = Math.max(maxLon, gp.getLongitudeE6());
minLat = Math.min(minLat, gp.getLatitudeE6());
minLon = Math.min(minLon, gp.getLongitudeE6());
}
// Find the spans and center point
double spanLat = Math.abs(maxLat - minLat);
double spanLon = Math.abs(maxLon - minLon);
int centerLat = (int) ((minLat + spanLat / 2d));
int centerLon = (int) ((minLon + spanLon / 2d));
GeoPoint center = new GeoPoint(centerLat, centerLon);
// Pan to center
mc.animateTo(center);
// Zoom to include all GeoPoints
mc.zoomToSpan((int)(spanLat), (int)(spanLon));