gpt4 book ai didi

objective-c - 如何删除 MKMapView 上的所有注释

转载 作者:IT老高 更新时间:2023-10-28 12:21:58 26 4
gpt4 key购买 nike

有没有一种简单的方法可以删除 map 上的所有注释,而无需遍历 Objective-c 中显示的所有注释?

最佳答案

是的,方法如下

[mapView removeAnnotations:mapView.annotations]

However the previous line of code will remove all map annotations "PINS" from the map, including the user location pin "Blue Pin". To remove all map annotations and keep the user location pin on the map, there are two possible ways to do that

Example 1, retain the user location annotation, remove all pins, add the user location pin back, but there is a flaw with this approach, it will cause the user location pin to blink on the map, due to removing the pin then adding it back

- (void)removeAllPinsButUserLocation1 
{
id userLocation = [mapView userLocation];
[mapView removeAnnotations:[mapView annotations]];

if ( userLocation != nil ) {
[mapView addAnnotation:userLocation]; // will cause user location pin to blink
}
}

Example 2, I personally prefer to avoid removing the location user pin in the first place,

- (void)removeAllPinsButUserLocation2
{
id userLocation = [mapView userLocation];
NSMutableArray *pins = [[NSMutableArray alloc] initWithArray:[mapView annotations]];
if ( userLocation != nil ) {
[pins removeObject:userLocation]; // avoid removing user location off the map
}

[mapView removeAnnotations:pins];
[pins release];
pins = nil;
}

关于objective-c - 如何删除 MKMapView 上的所有注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3027392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com