我想在Google地图中添加和删除Marker
(pin)。
我想用长触摸掉针并将其移除。我想用它来选择我的目的地。我该怎么做?
let position = CLLocationCoordinate2DMake(10, 10)
let marker = GMSMarker(position: position)
marker.map = mapView
对于那些正在寻找使用Swift的完整代码段的人:
GMSMapViewDelegate
@IBOutlet weak var googleMapView: GMSMapView!
的实例viewDidLoad()
中提及GMSMapView Delegate为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
}
这段代码可以帮到你!
//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
希望有所帮助!