4
A
回答
3
您可以通过的MKMapView的
[map region];
属性获取地图的缩放级别。你也通过实现MKMapViewDelegate方法和设置委托
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
在这里你可以查看你当前的缩放级别是什么得到区域变化的事件 通知。我不建议在缩放/平移时删除或添加所有注释,因为这可能会影响应用的性能。我没有真正尝试设置为0.0或MKAnnotationView上的隐藏属性,但这可能是你最好的选择。
4
使用Marko的答案我来到这个解决方案。
每次区域更改,我更改ViewController's
属性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
方法。
工程很好,我没有注意到与性能有关的大问题。虽然这可能会随着注释数量的增加而改变...
+0
由于视图会在您有很多注释时重复使用,因此for循环中的每个注释都不会保证有视图。所以如果你在缩放变化之后平移,你应该小心地使用正确的alpha属性创建视图 – Pieter 2016-08-26 11:25:13
请参阅我的anser下http://stackoverflow.com/questions/14707080/after-json-parsing-viewforannotation-shows-only-one-single-annotation- on-mkmapv/32965056#32965056希望可以帮助别人。 – blackjacx 2015-10-06 08:25:30