ios - Swift 3 - 长按 GMSMarker 并打印其坐标(Google Maps iOS SDK)

标签 ios google-maps swift3 long-press uilongpressgesturerecogni

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

问题是我在坐标字典中的所有坐标上都被“长按”。

我想要

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

我该如何解决这个问题?查看了其他解决方案,但没有解决太多问题。

最佳答案

我查看了 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/

相关文章:

ios - QuickBlox iOS 失败,错误代码为 422

Android 谷歌地图只有部分星标

Swift3.0 中的 Realm 意外状态代码 : -34018

android - 如何仅加载当前在屏幕上可见的标记(Android 上的 Google map )

ios - 类型 'GameScene has no member ' 延迟'

ios - 当用户用手指拾取时的动画表格 View 单元格(Swift 3)

ios - Firebase 不适用于真实设备 (iOS)

ios - MKPinAnnotationView:引脚宽度和高度

ios - 在我的子项目(CocoaTouch 框架)中使用第 3 方框架(Alamofire)

jquery - HTML5 离线谷歌地图访问