Swift - GoogleMaps SDK获取触摸坐标

Ste*_*ell 4 google-maps google-maps-markers google-maps-sdk-ios swift

我是swift和Google Maps SDK的新手,想知道如何使用Google Maps SDK获取用户点击的位置坐标.例如,如果用户将手指放在地图上的某个位置,则会在那里创建注释.我非常感谢你的帮助,谢谢.

Geo*_*ell 11

在GMSMapViewDelegate中有一个名为的方法:mapView:didLongPressAtCoordinate:在特定坐标的长按手势之后调用.请参阅此处的参考.

通过实现此方法,您可以在地图视图中添加标记:

func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
     let marker = GMSMarker(position: coordinate)
     marker.title = "Hello World"
     marker.map = mapView
}
Run Code Online (Sandbox Code Playgroud)

对于轻击手势,可以实现类似的委托方法mapView:didTapAtCoordinate:,其可以以类似的方式使用:

func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
     print("Tapped at coordinate: " + String(coordinate.latitude) + " " 
                                    + String(coordinate.longitude))
}
Run Code Online (Sandbox Code Playgroud)