Guest User

Untitled

a guest
Dec 14th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. //Swift 4.0
  2.  
  3. var mapView: BMKMapView!
  4.  
  5. func showAllLocations(locations: [CLLocationCoordinate2D]) {
  6. guard locations.count >= 1 else { return }
  7. var minLat = locations[0].latitude
  8. var maxLat = locations[0].latitude
  9. var minLon = locations[0].longitude
  10. var maxLon = locations[0].longitude
  11.  
  12. for location in locations {
  13. minLat = min(minLat, location.latitude)
  14. maxLat = max(maxLat, location.latitude)
  15. minLon = min(minLon, location.longitude)
  16. maxLon = max(maxLon, location.longitude)
  17. }
  18.  
  19. let padding = (maxLat - minLat) * 0.01 //padding according to (maxLat - minLat) value
  20. let center = CLLocationCoordinate2D(latitude: (minLat + maxLat) / 2, longitude: (minLon + maxLon) / 2)
  21. let span = BMKCoordinateSpan(latitudeDelta: maxLat - minLat + padding, longitudeDelta: maxLon - minLon + padding)
  22. let region = BMKCoordinateRegion(center: center, span: span)
  23. mapView.setRegion(region, animated: true)
  24. }
Add Comment
Please, Sign In to add comment