Last active
December 8, 2016 01:03
-
-
Save bluee/19b53190659b92ccf2c03c2551ef82a6 to your computer and use it in GitHub Desktop.
Xamarin.IOS ZoomToFitMapAnnoations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MapHelper | |
{ | |
public static void CenterMap(this MapKit.MKMapView map, CLLocationCoordinate2D mapCenter) | |
{ | |
map.SetRegion(MKCoordinateRegion.FromDistance(mapCenter, 1000, 1000), true); | |
} | |
public static void ZoomToFitMapAnnotations(this MapKit.MKMapView map) | |
{ | |
map.ShowAnnotations(map.Annotations, false); | |
} | |
public static void ZoomToFitMapCoordinates(this MapKit.MKMapView map, List<CLLocationCoordinate2D> coordinates) | |
{ | |
if (map.Annotations.Length <= 0) | |
return; | |
var topLeftCoord = new CLLocationCoordinate2D(-90, 180); | |
var bottomRightCoord = new CLLocationCoordinate2D(90, -180); | |
foreach (var coordinate in coordinates) | |
{ | |
topLeftCoord.Longitude = Math.Min(topLeftCoord.Longitude, coordinate.Longitude); | |
topLeftCoord.Latitude = Math.Max(topLeftCoord.Latitude, coordinate.Latitude); | |
bottomRightCoord.Longitude = Math.Max(bottomRightCoord.Longitude, coordinate.Longitude); | |
bottomRightCoord.Latitude = Math.Min(bottomRightCoord.Latitude, coordinate.Latitude); | |
} | |
double delta = 1.4; | |
MKCoordinateRegion region = new MKCoordinateRegion(); | |
region.Center.Latitude = topLeftCoord.Latitude - (topLeftCoord.Latitude - bottomRightCoord.Latitude) * 0.5; //0.5 | |
region.Center.Longitude = topLeftCoord.Longitude + (bottomRightCoord.Longitude - topLeftCoord.Longitude) * 0.5; //0.5 | |
region.Span.LatitudeDelta = Math.Abs((topLeftCoord.Latitude - bottomRightCoord.Latitude) * delta); | |
region.Span.LongitudeDelta = Math.Abs((bottomRightCoord.Longitude - topLeftCoord.Longitude) * delta); | |
region = map.RegionThatFits(region); | |
map.SetRegion(region, true); | |
} | |
} | |
// InvokeOnMainThread(() => { }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment