2016-06-07 15 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 }