我使用removeAnnotations
从mapView
删除我的注释,但删除了用户位置ann。我该如何防止这种情况发生,或者如何让用户回来查看?如何从MKMapView中除去用户位置注释以外的所有注释?
NSArray *annotationsOnMap = mapView.annotations;
[mapView removeAnnotations:annotationsOnMap];
我使用removeAnnotations
从mapView
删除我的注释,但删除了用户位置ann。我该如何防止这种情况发生,或者如何让用户回来查看?如何从MKMapView中除去用户位置注释以外的所有注释?
NSArray *annotationsOnMap = mapView.annotations;
[mapView removeAnnotations:annotationsOnMap];
更新:
当我尝试使用iOS 9 SDK时,不再删除用户注释。你可以简单地使用
mapView.removeAnnotations(mapView.annotations)
历史答案(针对iOS的前9 iOS上运行的应用程序):
试试这个:
NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
[ annotationsToRemove removeObject:mapView.userLocation ] ;
[ mapView removeAnnotations:annotationsToRemove ] ;
编辑:雨燕版
let annotationsToRemove = mapView.annotations.filter { $0 !== mapView.userLocation }
mapView.removeAnnotations(annotationsToRemove)
如果您的用户的位置是一种类MKUserLocation
的,使用isKindOfClass
避免删除用户的位置标注。
if (![annotation isKindOfClass:[MKUserLocation class]]) {
}
否则,你可以设置一个标志来识别一种注解的– mapView:viewForAnnotation:
。
谢谢!我会记住这个 –
嗨,试试这个我得到了解决方案从离子这段代码:
NSMutableArray*listRemoveAnnotations = [[NSMutableArray alloc] init];
[Mapview removeAnnotations:listRemoveAnnotations];
[listRemoveAnnotations release];
这并不回答这个问题。实际上,这个代码没有效果 - 当调用'-removeAnimations'时''listRemoveAnnotations'是空的。 – nielsbot
要清除所有地图中的注释:
[self.mapView removeAnnotations:[self.mapView annotations]];
从MapView的
for (id <MKAnnotation> annotation in self.mapView.annotations)
{
if (![annotation isKindOfClass:[MKUserLocation class]])
{
[self.mapView removeAnnotation:annotation];
}
}
希望这可以帮助您删除指定的注解。
这个答案比公认的简洁得多。 – shapeare
某些NSPredicate
过滤器如何?
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"className != %@", NSStringFromClass(MKUserLocation.class)];
NSArray *nonUserAnnotations = [self.mapView.annotations filteredArrayUsingPredicate:predicate];
[self.mapView removeAnnotations:nonUserAnnotations];
生活与NSPredicate总是更好的过滤
谢谢!这正是我需要的! –
Thanx ..... Buddy – user968597
也谢谢我:) –