【发布时间】:2019-02-15 09:03:28
【问题描述】:
首先,我向您保证,这不是一个重复的问题。我有一张地图,如果用户更改区域,我想从 X 方向更改我的注释坐标。我的目标是在视图的左侧创建我的图钉。
当我创建我的 pin 时,我将它添加到我的 var pin: MyCustomPin 中。但是,在我的 regionDidChangeAnimated;
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
calculateZoomFactor()
if isInLegalRegion() {
//.....
} else {
mapView.setCenter(mapCenter, animated: true)
}
arrangePins()
}
我的arrangePins() 函数可以;
func arrangePins() {
for pin in pins {
let coord = pin.coordinate
let title = pin.title!
self.mapView.removeAnnotation(pin)
let xCoord = self.mapView.visibleMapRect.minX + 250
var newCoord = MKMapPoint(coord)
newCoord.x = xCoord
let newPin = MyPin(newCoord.coordinate, title)
mapView.addAnnotation(newPin)
}
}
到目前为止一切顺利。虽然看起来非常合乎逻辑和简单,但我的旧注释并没有从地图中删除。有什么我想念的吗?
【问题讨论】:
-
从您的
mapView中删除旧注释。使用removeAnnotations方法。 -
我正在使用删除注释方法,如您所见.. 但是我无法删除所有注释.. self.mapView.removeAnnotation(pin) 您可以在我的arrangePins() 函数中看到此代码
-
我不想删除所有注释。你没看我的评论吗?
-
可能是 MapKit 中已有百万年历史的错误,请参阅 stackoverflow.com/questions/6876662/…
标签: ios swift mapkit mkannotation mkannotationview