0

使用 Google Maps iOS SDK,如果长按该特定标记,我希望能够打印 GMSMarker 的坐标。

我首先从字典中获取所有坐标,然后为地图上的所有坐标放置标记:

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)")
}

问题是我从坐标字典中“长按”了所有坐标。

我想要

  1. 在字典中放置所有坐标的标记
  2. 长按特定标记
  3. 并仅打印长按的特定标记的坐标。

我该怎么做?看看其他解决方案,无法解决很多问题。

4

2 回答 2

1

我查看了 GMSMapView API。有一个名为“didLongPressAtCoordinate”的方法传入 CLLocationCoordinate2D,所以我认为您可以使用它来创建 GMSMarker。看这里

你必须实现 GMSMapViewDelegate,你可以调用

func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
    let marker = GMSMarker(position: coordinate)
    marker.title = "Found You!"
    marker.map = mapView
}

希望这个有帮助:)

于 2018-03-11T00:52:50.283 回答
0

我以前做过。您基本上必须将地图上的触摸点转换为坐标。

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(userPerformedLongPress(gesture:)))
    uilpgr.minimumPressDuration = 2.0
}

func userPerformedLongPress(gesture: UIGestureRecognizer) {
    let touchPoint = gesture.location(in: mapView)
    let newCoordinate: CLLocationCoordinate2D = mapView.convert(touchPoint, toCoordinateFrom: mapView)
    let annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinate
    annotation.title = "Location Selected"
    annotation.subtitle = "Coordinate: \(round(1000*newCoordinate.longitude)/1000), \(round(1000*newCoordinate.latitude)/1000)"
    mapView.addAnnotation(annotation)
    print("Gesture recognized")
}
于 2018-03-10T09:37:58.263 回答