Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Created November 22, 2011 09:11
Show Gist options
  • Save Cheesebaron/1385260 to your computer and use it in GitHub Desktop.
Save Cheesebaron/1385260 to your computer and use it in GitHub Desktop.
Method to center around overlay items
private void CenterAroundOverlayItems()
{
if (itemizedOverlay.OverlayItems.Count == 0) return;
double hpadding = 0.1;
double vpadding = 0.15;
//start wide
int minLatitude = (int)(90 * 1E6);
int maxLatitude = (int)(-90 * 1E6);
int minLongitude = (int)(180 * 1E6);
int maxLongitude = (int)(-180 * 1E6);
foreach (OverlayItem item in itemizedOverlay.OverlayItems.ToArray())
{
int lat = item.Point.LatitudeE6;
int lon = item.Point.LongitudeE6;
//narrow down
maxLatitude = Math.Max(lat, maxLatitude);
minLatitude = Math.Min(lat, minLatitude);
maxLongitude = Math.Max(lon, maxLongitude);
minLongitude = Math.Min(lon, minLongitude);
}
maxLatitude = maxLatitude + (int)((maxLatitude - minLatitude) * hpadding);
minLatitude = minLatitude - (int)((maxLatitude - minLatitude) * hpadding);
maxLongitude = maxLongitude + (int)((maxLongitude - minLongitude) * vpadding);
minLongitude = minLongitude - (int)((maxLongitude - minLongitude) * vpadding);
mapView.Controller.ZoomToSpan(maxLatitude - minLatitude, maxLongitude - minLongitude); //this will zoom to nearest zoom level
mapView.Controller.AnimateTo( //go to the center of the span
new GeoPoint(
(int)((maxLatitude + minLatitude) / 2),
(int)((maxLongitude + minLongitude) / 2)
));
}
@Cheesebaron
Copy link
Author

The ToArray() call is because it creates a copy of the itemized overlay object which allows to read and write to it at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment