我有一个MKMapView,其中包含一些自定义注释,当地图缩放到一定程度时,它们看起来不是很好。
是否可以在地图达到特定缩放级别时仅显示/添加它们?
我有一个MKMapView,其中包含一些自定义注释,当地图缩放到一定程度时,它们看起来不是很好。
是否可以在地图达到特定缩放级别时仅显示/添加它们?
我参考了Marko的答案,得出了以下解决方案。
每次区域发生变化时,我会更改ViewController
的属性isAtBigZoom
。
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
isAtBigZoom = mapView.region.span.latitudeDelta < 0.01
}
然后在属性的didSet
中,我执行了这段代码。
var isAtBigZoom = false {
didSet {
// this guard ensures, that the showing and hiding happens only once
guard oldValue != isAtBigZoom else {
return
}
// in my case I wanted to show/hide only a certain type of annotations
for case let annot as MapTextAnnotation in mapView.annotations {
mapView.viewForAnnotation(annot)?.alpha = isAtBigZoom ? 1 : 0
}
}
}
viewForAnnotation
方法中即可。它很好用,性能方面我没有发现大问题。虽然随着注释数量的增加,情况可能会有所改变...您可以通过以下方式获取地图缩放级别:
[map region];
MKMapView的属性。通过实现MKMapViewDelegate方法并设置代理,您还可以获取区域更改事件的通知。
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
在这里,您可以检查当前的缩放级别。我不建议在缩放/平移时删除或添加所有注释,因为那会严重影响应用程序的性能。我还没有真正尝试过在MKAnnotationView上设置alpha为0.0或隐藏属性,但这可能是最好的选择。