Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Google Maps Center on Marker Click

Tags:

swift

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?

like image 317
Rupert Avatar asked Aug 06 '15 02:08

Rupert


2 Answers

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)
like image 198
Maryam Fekri Avatar answered Oct 04 '22 17:10

Maryam Fekri


in swift4 for click on Marker:

 func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
     // your code.
 }
like image 41
Mohamad.j Avatar answered Oct 04 '22 17:10

Mohamad.j