I am trying to center the Google map on the marker and zoom when the user taps a marker. I am able to do this in Android using a the click delegate, but I can't find a way to do this in Swift.
Does anyone have any hints or advice to solve this issue?
You have to conform the GMSMapViewDelegate protocol , then you have the func below:
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
//you can handle zooming and camera update here
}
you can update the camera by creating bound for more than one position like : ( also you can give a padding to the bound)
let bounds = GMSCoordinateBounds(coordinate: self.userLocation!.coordinate, coordinate: marker.position)
self.mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 120.0))
and also you can update the camera position with one position with a zoom level like :
self.mapView?.camera = GMSCameraPosition.cameraWithTarget(marker.position, zoom: 9.0)
in swift4 for click on Marker:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
// your code.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With