26

自分の位置の青い点を除いて、マップビューからすべての注釈を削除したいと思います。私が電話するとき:

[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]]; 
       } 
    }
4

7 に答える 7

58

MKMapView documentationを見ると、遊ぶための注釈プロパティがあるようです。これを反復して、どのような注釈があるかを確認するのは非常に簡単です。

for (id annotation in myMap.annotations) {
    NSLog(@"%@", annotation);
}

userLocationユーザーの位置を表す注釈を提供するプロパティもあります。removeAnnotations:注釈を調べて、ユーザーの場所ではないすべてを覚えている場合は、メソッドを使用してそれらを削除できます。

NSInteger toRemoveCount = myMap.annotations.count;
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:toRemoveCount];
for (id annotation in myMap.annotations)
    if (annotation != myMap.userLocation)
        [toRemove addObject:annotation];
[myMap removeAnnotations:toRemove];

お役に立てれば、

サム

于 2010-01-25T12:38:40.093 に答える
31

すばやく簡単にしたい場合は、MKUserLocation アノテーションの配列をフィルタリングする方法があります。これを MKMapView の removeAnnotations: 関数に渡すことができます。

 [_mapView.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"!(self isKindOfClass: %@)", [MKUserLocation class]]];

これは、述語を使用して汚い作業を行うことを除いて、上記の手動フィルターとほとんど同じであると思います。

于 2010-05-26T16:50:46.630 に答える
13

次のことを行う方が簡単ではありませんか?

//copy your annotations to an array
    NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray: mapView.annotations]; 
//Remove the object userlocation
    [annotationsToRemove removeObject: mapView.userLocation]; 
 //Remove all annotations in the array from the mapView
    [mapView removeAnnotations: annotationsToRemove];
    [annotationsToRemove release];
于 2012-03-22T17:28:26.177 に答える
6
for (id annotation in map.annotations) {
    NSLog(@"annotation %@", annotation);

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

        [map removeAnnotation:annotation];
    }
    }

私はこのように修正しました

于 2011-08-16T06:05:58.040 に答える
1

次のことを行う方が簡単です。

NSMutableArray *annotationsToRemove = [NSMutableArray arrayWithCapacity:[self.mapView.annotations count]];
    for (int i = 1; i < [self.mapView.annotations count]; i++) {
        if ([[self.mapView.annotations objectAtIndex:i] isKindOfClass:[AddressAnnotation class]]) {
            [annotationsToRemove addObject:[self.mapView.annotations objectAtIndex:i]];
            [self.mapView removeAnnotations:annotationsToRemove];
        }
    }

[self.mapView removeAnnotations:annotationsToRemove];
于 2012-12-13T13:23:12.563 に答える