Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. public void zoomToFitMapAnnotations()
  2. {
  3. if (map.Annotations.Length <= 0)
  4. return;
  5.  
  6. var topLeftCoord = new CLLocationCoordinate2D(-90, 180);
  7. var bottomRightCoord = new CLLocationCoordinate2D(90, -180);
  8.  
  9. foreach (var marker in map.Annotations)
  10. {
  11. topLeftCoord.Longitude = Math.Min(topLeftCoord.Longitude, marker.Coordinate.Longitude);
  12. topLeftCoord.Latitude = Math.Max(topLeftCoord.Latitude, marker.Coordinate.Latitude);
  13.  
  14. bottomRightCoord.Longitude = Math.Max(bottomRightCoord.Longitude, marker.Coordinate.Longitude);
  15. bottomRightCoord.Latitude = Math.Min(bottomRightCoord.Latitude, marker.Coordinate.Latitude);
  16.  
  17. }
  18.  
  19. double delta = 1.4;
  20. MKCoordinateRegion region = new MKCoordinateRegion();
  21. region.Center.Latitude = topLeftCoord.Latitude - (topLeftCoord.Latitude - bottomRightCoord.Latitude)*0.5; //0.5
  22. region.Center.Longitude = topLeftCoord.Longitude + (bottomRightCoord.Longitude - topLeftCoord.Longitude)*0.5; //0.5
  23. region.Span.LatitudeDelta = Math.Abs((topLeftCoord.Latitude - bottomRightCoord.Latitude)* delta);
  24. region.Span.LongitudeDelta = Math.Abs((bottomRightCoord.Longitude - topLeftCoord.Longitude) * delta);
  25.  
  26. region = map.RegionThatFits(region);
  27. map.SetRegion(region, true);
  28. }
  29.  
  30.  
  31. private void CenterMap(CLLocationCoordinate2D mapCenter)
  32. {
  33. InvokeOnMainThread(() =>
  34. {
  35. map.SetRegion(MKCoordinateRegion.FromDistance(mapCenter, 1000, 1000), true);
  36. });
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement