如何在缩小地图视图时隐藏注释。我有大量的注释我必须隐藏它们,因为如果地图上显示的区域太大,你只能看到注释。
答案 0 :(得分:16)
为此,您必须检查您所在地区的大小,并根据它设置隐藏或不隐藏的视图。
我测试了下面的代码但是,您可能需要进行一些调整。
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSArray *annotations = [_mapView annotations];
MyAnnotation *annotation = nil;
for (int i=0; i<[annotations count]; i++)
{
annotation = (MyAnnotation*)[annotations objectAtIndex:i];
if (_mapView.region.span.latitudeDelta > .010)
{
[[_mapView viewForAnnotation:annotation] setHidden:YES];
}
else {
[[_mapView viewForAnnotation:annotation] setHidden:NO];
}
}
}
干杯,
VFN
答案 1 :(得分:9)
Swift版本:
let annotations = self.maps.annotations
for annotation in annotations
{
if (self.maps.region.span.latitudeDelta > 0.010)
{
self.maps.viewForAnnotation(annotation)?.hidden = true
}
else {
self.maps.viewForAnnotation(annotation)?.hidden = false
}
}