2016-06-07 42 views
1

我想在Google地图中添加和删除Marker(pin)。当谷歌地图swift用户触摸屏时放下一个PIN

我想长时间放下引脚并将其删除。我想用它来选择我的目的地。我该怎么做?

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

回答

2

此代码应该对您有所帮助!

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

希望有帮助!

2

对于谁正在寻找使用斯威夫特一个完整的代码片段的那些:

  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 }

相关问题