除了用户位置注释之外,如何从MKMapView中删除所有注释?

时间:2022-09-24 23:34:03

I use removeAnnotations to remove my annotations from mapView but same it remove user location ann. How can I prevent this, or how to get user ann back to view?

我使用removeAnnotations从mapView中删除我的注释,但同样删除用户位置ann。如何防止这种情况,或者如何让用户回来查看?

NSArray *annotationsOnMap = mapView.annotations;
        [mapView removeAnnotations:annotationsOnMap];

6 个解决方案

#1


84  

Update:

更新:

When I tried with the iOS 9 SDK the user annotation is no longer removed. You can simply use

当我尝试使用iOS 9 SDK时,不再删除用户注释。你可以简单地使用

mapView.removeAnnotations(mapView.annotations)

Historical answer (for apps that run on iOS before iOS 9):

历史答案(适用于在iOS 9之前在iOS上运行的应用):

Try this:

尝试这个:

NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
[ annotationsToRemove removeObject:mapView.userLocation ] ;
[ mapView removeAnnotations:annotationsToRemove ] ;

EDIT: Swift version

编辑:Swift版本

let annotationsToRemove = mapView.annotations.filter { $0 !== mapView.userLocation }
mapView.removeAnnotations( annotationsToRemove )

#2


19  

To clear all the annotations from the map:

要清除地图中的所有注释:

[self.mapView removeAnnotations:[self.mapView annotations]];

To remove specified annotations from Mapview

从Mapview中删除指定的注释

 for (id <MKAnnotation> annotation in self.mapView.annotations)
{
    if (![annotation isKindOfClass:[MKUserLocation class]])
    {
              [self.mapView removeAnnotation:annotation];   
    }

}

Hope this may help you.

希望这可以帮到你。

#3


6  

For Swift you can simply use a one-liner:

对于Swift,你可以简单地使用单行:

mapView.removeAnnotations(mapView.annotations)

Edit: As nielsbot mentioned it will also remove the user's location annotation unless you have set it up like this:

编辑:正如nielsbot提到的,除非你像这样设置它,否则它也将删除用户的位置注释:

mapView.showsUserLocation = true

#4


3  

If your user location is kind of class of MKUserLocation, use isKindOfClass to avoid removing user location annotation.

如果您的用户位置是MKUserLocation的类,请使用isKindOfClass以避免删除用户位置注释。

if (![annotation isKindOfClass:[MKUserLocation class]]) {

}

Else you can set a flag to recognize the kind of your annotations in – mapView:viewForAnnotation:.

否则,你可以设置一个标志来识别你的注释类型 - mapView:viewForAnnotation:。

#5


0  

How about some NSPredicate filter?

一些NSPredicate过滤器怎么样?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"className != %@", NSStringFromClass(MKUserLocation.class)];
NSArray *nonUserAnnotations = [self.mapView.annotations filteredArrayUsingPredicate:predicate];
[self.mapView removeAnnotations:nonUserAnnotations];

Life is always better with NSPredicate filter

使用NSPredicate过滤器,生活总是更好

#6


-1  

Hi try this i got the solution from this code:

嗨试试这个我得到了这段代码的解决方案:

 NSMutableArray*listRemoveAnnotations = [[NSMutableArray alloc] init];
[Mapview removeAnnotations:listRemoveAnnotations];

 [listRemoveAnnotations release];

#1


84  

Update:

更新:

When I tried with the iOS 9 SDK the user annotation is no longer removed. You can simply use

当我尝试使用iOS 9 SDK时,不再删除用户注释。你可以简单地使用

mapView.removeAnnotations(mapView.annotations)

Historical answer (for apps that run on iOS before iOS 9):

历史答案(适用于在iOS 9之前在iOS上运行的应用):

Try this:

尝试这个:

NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
[ annotationsToRemove removeObject:mapView.userLocation ] ;
[ mapView removeAnnotations:annotationsToRemove ] ;

EDIT: Swift version

编辑:Swift版本

let annotationsToRemove = mapView.annotations.filter { $0 !== mapView.userLocation }
mapView.removeAnnotations( annotationsToRemove )

#2


19  

To clear all the annotations from the map:

要清除地图中的所有注释:

[self.mapView removeAnnotations:[self.mapView annotations]];

To remove specified annotations from Mapview

从Mapview中删除指定的注释

 for (id <MKAnnotation> annotation in self.mapView.annotations)
{
    if (![annotation isKindOfClass:[MKUserLocation class]])
    {
              [self.mapView removeAnnotation:annotation];   
    }

}

Hope this may help you.

希望这可以帮到你。

#3


6  

For Swift you can simply use a one-liner:

对于Swift,你可以简单地使用单行:

mapView.removeAnnotations(mapView.annotations)

Edit: As nielsbot mentioned it will also remove the user's location annotation unless you have set it up like this:

编辑:正如nielsbot提到的,除非你像这样设置它,否则它也将删除用户的位置注释:

mapView.showsUserLocation = true

#4


3  

If your user location is kind of class of MKUserLocation, use isKindOfClass to avoid removing user location annotation.

如果您的用户位置是MKUserLocation的类,请使用isKindOfClass以避免删除用户位置注释。

if (![annotation isKindOfClass:[MKUserLocation class]]) {

}

Else you can set a flag to recognize the kind of your annotations in – mapView:viewForAnnotation:.

否则,你可以设置一个标志来识别你的注释类型 - mapView:viewForAnnotation:。

#5


0  

How about some NSPredicate filter?

一些NSPredicate过滤器怎么样?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"className != %@", NSStringFromClass(MKUserLocation.class)];
NSArray *nonUserAnnotations = [self.mapView.annotations filteredArrayUsingPredicate:predicate];
[self.mapView removeAnnotations:nonUserAnnotations];

Life is always better with NSPredicate filter

使用NSPredicate过滤器,生活总是更好

#6


-1  

Hi try this i got the solution from this code:

嗨试试这个我得到了这段代码的解决方案:

 NSMutableArray*listRemoveAnnotations = [[NSMutableArray alloc] init];
[Mapview removeAnnotations:listRemoveAnnotations];

 [listRemoveAnnotations release];