拖动时更新 MKPointAnnotation object

Update MKPointAnnotation object while dragging

我想知道如何在拖动时更新我的​​ annotation 标题,因为所有 annotation 属性都只是 get

我试过的代码:

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
    }

}

MKMapViewDelegate的拖拽方法中,注解是只读的,所以可以使用MKMapViewDelegatedidSelectAnnotationView方法,因为在拖拽之前会调用didSelectAnnotationView方法,对于声明一个 selectedAnnotation 实例 属性 类型 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
    }

}