ios 如何从 MKMapView 中删除除用户位置注释之外的所有注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10865088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I remove all annotations from MKMapView except the user location annotation?
提问by Pavel Kaljunen
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。如何防止这种情况发生,或者如何让用户 ann 重新查看?
NSArray *annotationsOnMap = mapView.annotations;
[mapView removeAnnotations:annotationsOnMap];
回答by nielsbot
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 { [self.mapView removeAnnotations:[self.mapView annotations]];
!== mapView.userLocation }
mapView.removeAnnotations( annotationsToRemove )
回答by Aswathy Bose
To clear all the annotations from the map:
要清除地图中的所有注释:
for (id <MKAnnotation> annotation in self.mapView.annotations)
{
if (![annotation isKindOfClass:[MKUserLocation class]])
{
[self.mapView removeAnnotation:annotation];
}
}
To remove specified annotations from Mapview
从 Mapview 中删除指定的注释
mapView.removeAnnotations(mapView.annotations)
Hope this may help you.
希望这可以帮助你。
回答by Paradisetronic.com
For Swift you can simply use a one-liner:
对于 Swift,您可以简单地使用单行:
mapView.showsUserLocation = true
Edit: As nielsbot mentioned it will also remove the user's location annotation unless you have set it up like this:
编辑:正如 nielsbot 提到的,它还会删除用户的位置注释,除非您像这样设置:
if (![annotation isKindOfClass:[MKUserLocation class]]) {
}
回答by bradley
If your user location is kind of class of MKUserLocation
, use isKindOfClass
to avoid removing user location annotation.
如果您的用户位置属于 类MKUserLocation
,请使用isKindOfClass
以避免删除用户位置注释。
mapView.removeAnnotations(mapView.annotations.filter { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"className != %@", NSStringFromClass(MKUserLocation.class)];
NSArray *nonUserAnnotations = [self.mapView.annotations filteredArrayUsingPredicate:predicate];
[self.mapView removeAnnotations:nonUserAnnotations];
!== mapView.userLocation })
Else you can set a flag to recognize the kind of your annotations in – mapView:viewForAnnotation:
.
否则,您可以设置一个标志来识别– mapView:viewForAnnotation:
.
回答by Kuldeep Singh
Swift 4.2 or later
Swift 4.2 或更高版本
Add this line before adding the annotations
在添加注释之前添加这一行
extension MKMapView {
var annotationsNoUserLocation : [MKAnnotation] {
get {
return self.annotations.filter{ !( NSMutableArray*listRemoveAnnotations = [[NSMutableArray alloc] init];
[Mapview removeAnnotations:listRemoveAnnotations];
[listRemoveAnnotations release];
is MKUserLocation) }
}
}
func showAllAnnotations() {
self.showAnnotations(self.annotations, animated: true)
}
func removeAllAnnotations() {
self.removeAnnotations(self.annotations)
}
func showAllAnnotationsNoUserLocation() {
self.showAnnotations(self.annotationsNoUserLocation, animated: true)
}
}
回答by Laszlo
How about some NSPredicate
filter?
来个NSPredicate
过滤器怎么样?
Life is always better with NSPredicate filter
使用 NSPredicate 过滤器,生活总是更好
回答by madx
In Swift 4.1:
在Swift 4.1 中:
Normally if you don't want to remove your MKUserLocationannotation you can simply run:
通常,如果您不想删除MKUserLocation注释,则只需运行:
self.mapView.removeAnnotations(self.annotations)
.
self.mapView.removeAnnotations(self.annotations)
.
This method by default does not remove the MKUserLocationannotation from the annotations
list.
默认情况下,此方法不会从列表中删除MKUserLocation注释annotations
。
However if you need to filter out all annotations except the MKUserLocation(see annotationsNoUserLocation
variable below) for any other reason, like centering on all the annotations but the MKUserLocationannotation you can use this simple extension below.
但是,如果您出于任何其他原因需要过滤掉除MKUserLocation(参见annotationsNoUserLocation
下面的变量)之外的所有注释,例如以所有注释为中心,但MKUserLocation注释,您可以使用下面的这个简单扩展。
回答by madesh
Hi try this i got the solution from this code:
嗨,试试这个,我从这段代码中得到了解决方案:
##代码##