如何从 MKMapView 中删除所有注释而不删除蓝点?
我想从地图视图中删除所有注释,而不包含我位置的蓝点。当我调用时:
[mapView removeAnnotations:mapView.annotations];
所有注释都被删除。
如果注释不是蓝点注释,我可以通过什么方式检查(例如对所有注释进行 for 循环)?
编辑(我已经解决了这个问题):
for (int i =0; i < [mapView.annotations count]; i++) {
if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotationClass class]]) {
[mapView removeAnnotation:[mapView.annotations objectAtIndex:i]];
}
}
I would like to remove all annotations from my mapview without the blue dot of my position. When I call:
[mapView removeAnnotations:mapView.annotations];
all annotations are removed.
In which way can I check (like a for loop on all the annotations) if the annotation is not the blue dot annotation?
EDIT (I've solved with this):
for (int i =0; i < [mapView.annotations count]; i++) {
if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotationClass class]]) {
[mapView removeAnnotation:[mapView.annotations objectAtIndex:i]];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
查看 MKMapView 文档,它似乎您可以使用注释属性。迭代它并查看您拥有哪些注释应该非常简单:
您还拥有
userLocation
属性,它为您提供表示用户位置的注释。如果您浏览完注释并记住所有非用户位置的注释,则可以使用removeAnnotations:
方法删除它们:希望这会有所帮助,
Sam
Looking at the MKMapView documentation, it seems like you have the annotations property to play with. It should be pretty simple to iterate through this and see what annotations you have :
You also have the
userLocation
property which gives you the annotation representing the user's location. If you go through the annotations and remember all of them which are not the user location, you can then remove them using theremoveAnnotations:
method :Hope this helps,
Sam
如果您喜欢快速简单,可以使用一种方法来过滤 MKUserLocation 注释的数组。您可以将其传递到 MKMapView 的 removeAnnotations: 函数中。
我认为这与上面发布的手动过滤器几乎相同,除了使用谓词来完成肮脏的工作。
If you like quick and simple, there's a way to filter an array of the MKUserLocation annotation. You can pass this into MKMapView's removeAnnotations: function.
I assume this is pretty much the same as the manual filters posted above, except using a predicate to do the dirty work.
只需执行以下操作不是更容易吗:
Isn't it easier to just do the following:
清理所有注释并保留 MKUserLocation 类注释的最短方法
shortest way to clean all annotations and preserving MKUserLocation class annotation
我这样修改的
i modified like this
执行以下操作会更容易:
it easier to just do the following:
对于斯威夫特 3.0
For Swift 3.0