はい、[mapView setRegion: ...]
を入力すると、何らかの理由でmapView
の注釈が変更された場合、選択した注釈は選択解除されます(削除されようとしています)。
これを修正する方法の1つは、アノテーションの 'diff'置換を行うことです。
func displayNewMapPins(pinModels: [MyCustomPinModel]) {
self.mapView.removeAnnotations(self.mapView.annotations) //remove all of the currently displayed annotations
let newAnnotations = annotationModels.map { $0.toAnnotation } //convert 'MyCustomPinModel' to an 'MKAnnotation'
self.mapView.addAnnotations(newAnnotations) //put the new annotations on the map
}
あなたはもっとこのようなことにそれを変更したい:例えば、現時点ではあなたは(スウィフトで表される)のように見えるいくつかのコードを持っているかもしれません
func displayNewMapPins(pinModels: [MyCustomPinModel]) {
let oldAnnotations = self.mapView.annotations
let newAnnotations = annotationModels.map { $0.toAnnotation }
let annotationsToRemove = SomeOtherThing.thingsContainedIn(oldAnnotations, butNotIn: newAnnotations)
let annotationsToAdd = SomeOtherThing.thingsContainedIn(newAnnotations, butNotIn: oldAnnotations)
self.mapView.removeAnnotations(annotationsToRemove)
self.mapView.addAnnotations(annotationsToAdd)
}
SomeOtherThing.thingsContainedIn(:butNotIn:)
の正確な実装あなたの要求に依存しますが、これはあなたが目指したい一般的なコード構造です。
このようにすると、アプリのパフォーマンスが向上するという追加の利点があります。MKMapView
の注釈を追加したり削除したりするのは本当に高価なことがあります。