Reputation: 17373
I'm using google map and I set markers to my map like this:
var marker = GMSMarker(position: CLLocationCoordinate2D(latitude: Double(item.lat)!, longitude: Double(item.lon)!))
marker.map = mapview
now,I would like to detect when user click on these markers.
How can I do?
Upvotes: 3
Views: 2385
Reputation: 1039
For Swift 3
You can implement GMSMapViewDelegate
something like this:
extension YourViewConytoller: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print ("MarkerTapped Locations: \(marker.position.latitude), \(marker.position.longitude)")
return true
}
}
Upvotes: 4
Reputation: 2668
you should set your mapview
delegate to self
UIViewController
in viewDidLoad
self.mapview.delegate = self
your UIViewController
should
extension ViewControllerClass: GMSMapViewDelegate {
//class code
@objc(mapView:didTapMarker:) func mapView(_: GMSMapView, didTap marker: GMSMarker) -> Bool {
//do something
return true
}
}
maybe this method can be implemented some other way already, but Xcode
forced me to make it this way while migrating from Swift 2
to Swift 3
Upvotes: 6