Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Swift 4.0
- var mapView: BMKMapView!
- func showAllLocations(locations: [CLLocationCoordinate2D]) {
- guard locations.count >= 1 else { return }
- var minLat = locations[0].latitude
- var maxLat = locations[0].latitude
- var minLon = locations[0].longitude
- var maxLon = locations[0].longitude
- for location in locations {
- minLat = min(minLat, location.latitude)
- maxLat = max(maxLat, location.latitude)
- minLon = min(minLon, location.longitude)
- maxLon = max(maxLon, location.longitude)
- }
- let padding = (maxLat - minLat) * 0.01 //padding according to (maxLat - minLat) value
- let center = CLLocationCoordinate2D(latitude: (minLat + maxLat) / 2, longitude: (minLon + maxLon) / 2)
- let span = BMKCoordinateSpan(latitudeDelta: maxLat - minLat + padding, longitudeDelta: maxLon - minLon + padding)
- let region = BMKCoordinateRegion(center: center, span: span)
- mapView.setRegion(region, animated: true)
- }
Add Comment
Please, Sign In to add comment