使用 Google Maps iOS SDK,如果长按该特定标记,我希望能够打印 GMSMarker 的坐标。
我首先从字典中获取所有坐标,并在 map 上放置所有坐标的标记:
func placeMapMarkers() {
for item in self.finalDictionary as [Dictionary<String, String>] {
let lat = item["lat"] as! String
let lon = item["lon"] as! String
let SpotLat = Double(lat)
let SpotLon = Double(lon)
let SpotLocation = CLLocationCoordinate2DMake(SpotLat!, SpotLon!)
DispatchQueue.main.async(execute: {
self.SpotMarker = GMSMarker(position: SpotLocation)
self.SpotMarker?.icon = self.imageWithImage(image: UIImage(named: "SpotIcon")!, scaledToSize: CGSize(width: 35.0, height: 35.0))
self.SpotMarker?.title = "Long press to navigate here"
self.SpotMarker?.map = self.mapView
})
longPress(mapView: self.mapView, didLongPressAtCoordinate: SpotLocation)
}
}
我的 longPress 函数调用是在上面的 placeMapMarkers 函数本身中,因为我想在放置标记时识别自己是否长按了特定标记(我的想法可能是错误的)。
我的长按功能如下。
//This is long Press function:-
func longPressView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
//Here handle your long press on map marker like:-
print("long pressed at \(coordinate)")
}
问题是我在坐标字典中的所有坐标上都被“长按”。
我想要
- 为字典中的所有坐标放置标记
- 长按特定标记
- 并仅打印长按的特定标记的坐标。
我该如何解决这个问题?查看了其他解决方案,但没有解决太多问题。
最佳答案
我查看了 GMSMapView API。有一个名为“didLongPressAtCoordinate”的方法传入 CLLocationCoordinate2D,因此我认为您可以使用它来创建 GMSMarker。 See here
您必须实现 GMSMapViewDelegate,然后您可以调用
func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
let marker = GMSMarker(position: coordinate)
marker.title = "Found You!"
marker.map = mapView
}
希望这对您有所帮助:)
关于ios - Swift 3 - 长按 GMSMarker 并打印其坐标(Google Maps iOS SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49207374/