Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 509
S.M_Emamian Avatar asked Dec 17 '25 21:12

S.M_Emamian


2 Answers

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

like image 138
JuicyFruit Avatar answered Dec 20 '25 12:12

JuicyFruit


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
    }
}
like image 36
Santiago Carmona González Avatar answered Dec 20 '25 11:12

Santiago Carmona González



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!