【发布时间】:2018-11-03 18:29:17
【问题描述】:
我有一个 MapView,用户可以选择一个半径来显示一个区域。我添加一个 MKCircle 作为覆盖。当半径从 30 英里变为 1 英里时,当 MapView 放大时,MKCircle 的周边会出现明显的故障。故障看起来有点像天赋。只有在放大而不是缩小时才会发生。\
由于缩放不断变化,我在添加另一个覆盖之前删除了旧的覆盖,但我认为这不是问题。
当 MapView 的缩放发生变化时,如何消除圆圈上的故障?
@IBAction func newRadiusButtonTapped(sender: UIButton) {
// coordinate is the users location and span was 10 miles now it's 1 mile
let region = MKCoordinateRegionMake(location.coordinate, span)
mapView.setRegion(region, animated: true)
let circle = MKCircle(center: location.coordinate, radius: radius)
// remove old overlay before adding another one
for overlay in mapView.overlays {
mapView.remove(overlay)
}
view.layoutIfNeeded()
// mapView.layoutIfNeeded() I tried this but it didn't make a difference
mapView.add(circle)
}
【问题讨论】:
-
只是一个问题:你删除所有的覆盖,有必要吗?如果你之前存储过这个圈子,你只需要删除这个特殊的。通过删除 ALL 可能会产生一些副作用......还有一个经典问题:你是在主线程上做的吗?
-
我一直删除覆盖层的原因是因为当我没有删除它们时,每次按下按钮时都会在覆盖层之上添加覆盖层(mapView.add(circle) 正在添加它们)。我在没有 for 循环的情况下尝试了“mapView.remove(overlay)”,但没有任何效果。我还没有尝试将圆圈存储为类属性。我认为隐藏和取消隐藏它可能会让它看起来很奇怪。这是你的建议吗?
-
是的,是的,但据我所知,您可以将生成的圆圈存储在类属性中。有了这个参考,你可以删除它:所以有一个类属性“var Circle:MKCircle?”,在你的方法里面:“if circle != nil { mapView.remove(Circle)}”,并且:而不是“让 circle = MKCircle(..." 使用 "Circle = MKCircle( ..."。这应该可以工作..
-
@Hardy_Germany 我将它添加为类属性,在按下按钮时对其进行初始化,它没有任何区别。同样的故障。无论如何谢谢:)
-
mmhm 奇怪.. 你试过“func exchangeOverlay(MKOverlay, with: MKOverlay) 交换两个覆盖对象的位置。”有了这个,你可以用新的圆圈交换旧的圆圈......我经常使用这样的覆盖,我从来没有遇到过这样的故障......所以一定有它的原因。
标签: ios swift mkmapview mkcircle