【发布时间】:2011-03-02 22:15:07
【问题描述】:
有没有一种简单的方法可以删除地图上的所有注释,而无需遍历 Objective-c 中显示的所有注释?
【问题讨论】:
标签: objective-c iphone-sdk-3.0 annotations mkmapview
有没有一种简单的方法可以删除地图上的所有注释,而无需遍历 Objective-c 中显示的所有注释?
【问题讨论】:
标签: objective-c iphone-sdk-3.0 annotations mkmapview
是的,方法如下
[mapView removeAnnotations:mapView.annotations]
但是,上一行代码将删除所有地图注释“PINS” 地图,包括用户定位销“蓝销”。删除所有地图 注释并在地图上保留用户位置图钉,有两个 可能的方法来做到这一点
示例1,保留用户位置标注,去掉所有pin,添加 用户定位销,但这种方法有一个缺陷,它 将导致用户位置图钉在地图上闪烁,因为移除 引脚然后将其添加回来
- (void)removeAllPinsButUserLocation1
{
id userLocation = [mapView userLocation];
[mapView removeAnnotations:[mapView annotations]];
if ( userLocation != nil ) {
[mapView addAnnotation:userLocation]; // will cause user location pin to blink
}
}
示例 2,我个人更喜欢避免删除位置用户密码 首先,
- (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;
}
【讨论】:
这是最简单的方法:
-(void)removeAllAnnotations
{
//Get the current user location annotation.
id userAnnotation=mapView.userLocation;
//Remove all added annotations
[mapView removeAnnotations:mapView.annotations];
// Add the current user location annotation again.
if(userAnnotation!=nil)
[mapView addAnnotation:userAnnotation];
}
【讨论】:
以下是删除除用户位置之外的所有注释的方法,明确写出,因为我想我会再次寻找这个答案:
NSMutableArray *locs = [[NSMutableArray alloc] init];
for (id <MKAnnotation> annot in [mapView annotations])
{
if ( [annot isKindOfClass:[ MKUserLocation class]] ) {
}
else {
[locs addObject:annot];
}
}
[mapView removeAnnotations:locs];
[locs release];
locs = nil;
【讨论】:
这与 Sandip 的回答非常相似,只是它不会重新添加用户位置,因此蓝点不会再次闪烁。
-(void)removeAllAnnotations
{
id userAnnotation = self.mapView.userLocation;
NSMutableArray *annotations = [NSMutableArray arrayWithArray:self.mapView.annotations];
[annotations removeObject:userAnnotation];
[self.mapView removeAnnotations:annotations];
}
【讨论】:
您不需要保存对用户位置的任何引用。所需要的只是:
[mapView removeAnnotations:mapView.annotations];
只要您将mapView.showsUserLocation 设置为YES,您仍然会在地图上显示用户位置。将此属性设置为YES 基本上要求地图视图开始更新和获取用户位置,以在地图上显示它。来自MKMapView.hcmets:
// Set to YES to add the user location annotation to the map and start updating its location
【讨论】:
Swift 版本:
func removeAllAnnotations() {
let annotations = mapView.annotations.filter {
$0 !== self.mapView.userLocation
}
mapView.removeAnnotations(annotations)
}
【讨论】:
斯威夫特 3
if let annotations = self.mapView.annotations {
self.mapView.removeAnnotations(annotations)
}
【讨论】:
斯威夫特 2.0 简单而最好的:
mapView.removeAnnotations(mapView.annotations)
【讨论】:
要删除一种类型的子类,您可以这样做
mapView.removeAnnotations(mapView.annotations.filter({$0 is PlacesAnnotation}))
其中PlacesAnnotation 是MKAnnotation 的子类
【讨论】:
这是从 MKMapView 中删除所有标记以及所有路线(如果有)的功能:
func removeAppleMapOverlays() {
let overlays = self.appleMapView.overlays
self.appleMapView.removeOverlays(overlays)
let annotations = self.appleMapView.annotations.filter {
$0 !== self.appleMapView.userLocation
}
self.appleMapView.removeAnnotations(annotations)
}
干杯
【讨论】: