如何撤消 MapKit 注释拖动

How undo a MapKit annotation drag

如果在 mapView didChangeDragState 中,我检测到注释已被拖动到不需要的位置,我可以在 mapView:didChangeDragState.

中取消拖动
 case .Dragging:  
            let inColorado = StateOutline.inColorado(view.annotation!.coordinate)
            if !inColorado {
                view.dragState = .Canceling
            }

不幸的是,这会使图钉留在取消拖动的不需要的位置。

有人想将注释设置为

不允许设置坐标,因为 view.annotation!.coordinate 是一个只读 属性.

如何撤消注释拖动?

不考虑使用 MKAnnotation setCoordinate — 它已在 iOS 8.3 中删除。

唯一想到的是用新注释替换该注释并在其上设置坐标。理想情况下,引脚坐标将设置为其最后一个有效位置。

你在这里的错误是认为无法设置注释的坐标。它可以。 MKAnnotation 协议没有规定可设置的坐标,但所有实际采用者都有一个。只需使用 MKPointAnnotation。它的 coordinate 是可设置的。您甚至可能已经在使用一个!

public class MKPointAnnotation : MKShape {
    public var coordinate: CLLocationCoordinate2D
}

你甚至可以编写自己的 MKAnnotation 采用者:

import UIKit
import MapKit

class MyAnnotation : NSObject, MKAnnotation {
    dynamic var coordinate : CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    init(location coord:CLLocationCoordinate2D) {
        self.coordinate = coord
        super.init()
    }
}