S.M_Emamian
S.M_Emamian

Reputation: 17373

How to clickable for markers in google map

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

Answers (2)

For Swift 3

You can implement GMSMapViewDelegatesomething 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

JuicyFruit
JuicyFruit

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

Related Questions