【问题标题】:drop a pin when user touch screen in google maps swift当用户在谷歌地图中快速触摸屏时放下图钉
【发布时间】:2016-10-07 09:43:43
【问题描述】:

我想在 Google 地图中添加和删除 Marker(pin)。

我想通过长按放下图钉并将其移除。我想用它来选择我的目的地。我该怎么做?

let  position = CLLocationCoordinate2DMake(10, 10)
let marker = GMSMarker(position: position)
marker.map = mapView

【问题讨论】:

    标签: ios swift google-maps google-maps-markers


    【解决方案1】:

    对于那些正在寻找使用 Swift 的完整代码 sn-p 的人:

    1. 实现协议GMSMapViewDelegate
    2. 拖动一个 GMSMapView 实例@IBOutlet weak var googleMapView: GMSMapView!
    3. 将viewDidLoad() 中的 GMSMapView 委托称为googleMapView.delegate = self
    4. 实现didTapAt委托函数:

    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){ print("You tapped at \(coordinate.latitude), \(coordinate.longitude)") googleMapView.clear() // clearing Pin before adding new let marker = GMSMarker(position: coordinate) marker.map = googleMapView }

    【讨论】:

      【解决方案2】:

      这段代码应该可以帮到你!

      //MARK: GMSMapViewDelegate Implimentation.
      func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
          plotMarker(AtCoordinate: coordinate, onMapView: mapView)
      }
      
      //MARK: Plot Marker Helper
      private func plotMarker(AtCoordinate coordinate : CLLocationCoordinate2D, onMapView vwMap : GMSMapView) {
          let marker = GMSMarker(position: coordinate)
          marker.map = vwMap
      }
      

      PS:不要忘记在ViewController 中确认GMSMapViewDelegate 并在viewDidLoad() 中的某处分配googleMap.delegate = self

      希望有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 2015-07-23
        相关资源
        最近更新 更多