当谷歌地图swift用户触摸屏时放一个别针

问题描述 投票:3回答:2

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

我想用长触摸掉针并将其移除。我想用它来选择我的目的地。我该怎么做?

let  position = CLLocationCoordinate2DMake(10, 10)
let marker = GMSMarker(position: position)
marker.map = mapView
ios swift google-maps google-maps-markers
2个回答
4
投票

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

  1. 实施协议GMSMapViewDelegate
  2. 拖动GMSMapView @IBOutlet weak var googleMapView: GMSMapView!的实例
  3. viewDidLoad()中提及GMSMapView Delegate为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 }


3
投票

这段代码可以帮到你!

//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:别忘了在qazxsw poi向qazxsw poi确认并在GMSMapViewDelegate的某处指定qazxsw poi

希望有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.