【问题标题】: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 的人:
- 实现协议
GMSMapViewDelegate
- 拖动一个 GMSMapView 实例
@IBOutlet weak var googleMapView: GMSMapView!
- 将
viewDidLoad() 中的 GMSMapView 委托称为googleMapView.delegate = self
- 实现
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
希望有帮助!