【问题标题】:Swift 3 - LongPress on GMSMarker and print its coordinates (Google Maps iOS SDK)Swift 3 - 在 GMSMarker 上长按并打印其坐标(Google Maps iOS SDK)
【发布时间】:2018-03-10 09:15:45
【问题描述】:

使用 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. 并仅打印长按的特定标记的坐标。

我该怎么做?看了其他解决方案,没能解决多少。

【问题讨论】:

    标签: ios google-maps swift3 long-press uilongpressgesturerecogni


    【解决方案1】:

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

    希望这个有帮助:)

    【讨论】:

    • 我相信 OP 要求在已显示的标记上注册长按。
    【解决方案2】:

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

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

    【讨论】:

    • 我使用的是 GMSMarkers(谷歌地图 iOS SDK)而不是 MapKit。我将如何使用 Google Maps iOS SDK 来解决这个问题?
    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多