2017-04-01 69 views
2

标记我使用谷歌地图和我设置标记,以我的地图是这样的:如何点击在谷歌地图

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


marker.map = mapview 
现在

,我想,以检测这些标记,当用户点击。

我该怎么办?

回答

3

您应将mapview委托设置为selfUIViewControllerviewDidLoad

self.mapview.delegate = self 

UIViewController应该

extension ViewControllerClass: GMSMapViewDelegate { 
    //class code 

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

也许可以实现这个方法一些其他的方式了,但Xcode强迫我做它这种方式同时从Swift 2迁移到Swift 3

2

对于斯威夫特3

您可以实现GMSMapViewDelegate是这样的:

extension YourViewConytoller: GMSMapViewDelegate { 
    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { 
     print ("MarkerTapped Locations: \(marker.position.latitude), \(marker.position.longitude)") 
     return true 
    } 
}