How to fit bounds for coordinate array with google maps sdk for iOS? I need to zoom map for 4 visible markers.
Swift 3.0 version of Lirik's answer:
func focusMapToShowAllMarkers() {
let myLocation: CLLocationCoordinate2D = self.markers.first!.position
var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: myLocation, coordinate: myLocation)
for marker in self.markers {
bounds = bounds.includingCoordinate(marker.position)
self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))
}
}
And here's my own way:
func focusMapToShowMarkers(markers: [GMSMarker]) {
guard let currentUserLocation = self.locationManager.location?.coordinate else {
return
}
var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: currentUserLocation,
coordinate: currentUserLocation)
_ = markers.map {
bounds = bounds.includingCoordinate($0.position)
self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))
}
}
And you can call my function above like so:
self.focusMapToShowMarkers(markers: [self.myLocationMarker, currentPokemonMarker])