2016-12-24 57 views

回答

0

有多種方法可以做到這一點。其中之一是在用戶長按地圖時添加標記。爲了檢測長按,實現這個委託方法:

func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) { 
    let marker = GMSMarker(position: coordinate) 
    // marker.isDraggable = true 
    // marker.appearAnimation = kGMSMarkerAnimationPop 
    marker.map = mapView 
    // marker.icon = GMSMarker.markerImage(with: UIColor.blue) 
} 

被註釋掉的線是可選的,因爲它們只設置標記的自定義屬性。還有更多,你可以自定義。

另外,如果您還沒有這樣做的話,加GMSMapViewDelegate到您的視圖控制器類的聲明:

class YourViewController: UIViewController, GMSMapViewDelegate { 

,並分配selfdelegate屬性:

let camera = GMSCameraPosition.camera(withLatitude: 0, longitude: 0, zoom: 3) 
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) 
    mapView.isMyLocationEnabled = true 
    view = mapView 

    mapView.delegate = self // add this line! 

另外,您可以在發生其他事件時添加標記。例如,您可以設置當用戶點擊一個UIBarBUttonItemUIButton添加標記。全取決於你!但添加按鈕的過程基本上是這兩行:

let marker = GMSMarker(position: coordinate) 
marker.map = mapView 
// mapView is the map that you want to add the marker to. If you are doing this outside a delegate method, use self.view 

您還可以考慮將標記添加到集合,以便稍後修改它們。

+0

如果你覺得我的答案回答你的問題,請考慮一下對對號接受它! @情慾 – Sweeper

相關問題