如何在谷歌地图中点击标记

S.M*_*ian 2 google-maps ios swift

我正在使用谷歌地图,并为我的地图设置标记,如下所示:

var marker = GMSMarker(position: CLLocationCoordinate2D(latitude: Double(item.lat)!, longitude: Double(item.lon)!))


 marker.map = mapview
Run Code Online (Sandbox Code Playgroud)

现在,我想检测用户何时点击这些标记。

我能怎么做?

Jui*_*uit 6

您应将设置mapview委托self UIViewControllerviewDidLoad

self.mapview.delegate = self
Run Code Online (Sandbox Code Playgroud)

UIViewController应该

extension ViewControllerClass: GMSMapViewDelegate {
    //class code

    @objc(mapView:didTapMarker:) func mapView(_: GMSMapView, didTap marker: GMSMarker) -> Bool {
        //do something
        return true
    }
}
Run Code Online (Sandbox Code Playgroud)

也许这种方法已经可以通过其他方式实现,但Xcode迫使我在从 迁移Swift 2Swift 3