2016-08-04 5 views
1

をドラッグしながら、私はすべてのannotationプロパティのみgetているので、私はそれをドラッグしていながら、私のannotationタイトルを更新する方法を知りたいです。私が試してみる更新MKPointAnnotationオブジェクト

コード:MKMapViewDelegateのドラッグ方法で

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { 

    switch (newState) { 
    case .starting: 
     //view.dragState = .dragging 
     print("dragging.....") 
    case .ending, .canceling: 
     // view.dragState = .none 
     let lat = view.annotation?.coordinate.latitude 
     let long = view.annotation?.coordinate.longitude 
     let coordinates = CLLocationCoordinate2D(latitude: lat!, longitude: long!) 
     print(" pin lat \(lat) long \(long)") 

     //Error here - title is get only property 
     view.annotation?.title = "New Location" 

    default: break 
    } 

} 

答えて

2

は、注釈があなたがあるため、ドラッグする前にMKMapViewDelegatedidSelectAnnotationView方法を使用することができますので、それは1つのselectedAnnotationインスタンスを宣言するために、それは、didSelectAnnotationViewメソッドを呼び出します読み取り専用ですタイプMKPointAnnotationのプロパティ。

var selectedAnnotation: MKPointAnnotation? 

didSelectAnnotationViewメソッドでこのプロパティを使用します。

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 
    selectedAnnotation = view.annotation as? MKPointAnnotation 
} 

はタイトルがこの注釈

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { 
    switch (newState) { 
     case .starting: 
      //view.dragState = .dragging 
      print("dragging.....") 
     case .ending, .canceling: 
      // view.dragState = .none 
      let lat = view.annotation?.coordinate.latitude 
      let long = view.annotation?.coordinate.longitude 
      let coordinates = CLLocationCoordinate2D(latitude: lat!, longitude: long!) 
      print(" pin lat \(lat) long \(long)") 
      self. selectedAnnotation?.title = "New Location" 
     default: break 
    } 

} 
+0

おかげで男を使用し変更するには!出来た。 – ankit

+0

ようこそ、ハッピーコーディング:) –

関連する問題