Jak usunąć wszystkie adnotacje na MKMapView

Czy istnieje prosty sposób na usunięcie wszystkich adnotacji na mapie bez iteracji przez wszystkie wyświetlane adnotacje w Objective-c?

Author: kevin Mendoza, 2010-06-12

8 answers

Tak, oto jak

[mapView removeAnnotations:mapView.annotations]

Jednak poprzednia linia kodu usunie wszystkie adnotacje map "piny" z mapa, w tym PIN lokalizacji użytkownika "niebieski Pin". Aby usunąć wszystkie mapy adnotacje i zachować pin lokalizacji użytkownika na mapie, są dwa możliwe sposoby na to

Przykład 1, zachowaj adnotację o lokalizacji użytkownika, usuń wszystkie piny, dodaj lokalizacja użytkownika Pin z powrotem, ale jest wada z tym podejściem, to spowoduje, że pin lokalizacji użytkownika zostanie blink na mapie, ze względu na usunięcie pin następnie dodaje go z powrotem

- (void)removeAllPinsButUserLocation1 
{
    id userLocation = [mapView userLocation];
    [mapView removeAnnotations:[mapView annotations]];

    if ( userLocation != nil ) {
        [mapView addAnnotation:userLocation]; // will cause user location pin to blink
    }
}

Przykład 2, osobiście wolę unikać usuwania PIN użytkownika lokalizacji w pierwszej kolejności

- (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;
}
 237
Author: RocketMan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-25 09:53:48

Oto najprostszy sposób, aby to zrobić:

-(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];
}
 36
Author: Sandip Sarkar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-15 10:59:07

Oto jak usunąć wszystkie adnotacje z wyjątkiem lokalizacji użytkownika, napisane wyraźnie, ponieważ wyobrażam sobie, że będę szukał tej odpowiedzi ponownie:

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;
 17
Author: Victor Van Hee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-29 07:02:00

Jest to bardzo podobne do odpowiedzi Sandipa, z wyjątkiem tego, że nie dodaje ponownie lokalizacji użytkownika, więc niebieska kropka nie mruga i nie wyłącza się ponownie.

-(void)removeAllAnnotations
{
    id userAnnotation = self.mapView.userLocation;

    NSMutableArray *annotations = [NSMutableArray arrayWithArray:self.mapView.annotations];
    [annotations removeObject:userAnnotation];

    [self.mapView removeAnnotations:annotations];
}
 13
Author: Chris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-20 23:14:08

Nie musisz zapisywać żadnego odniesienia do lokalizacji użytkownika. Wystarczy:

[mapView removeAnnotations:mapView.annotations]; 

I tak długo, jak masz mapView.showsUserLocation ustawione na YES, nadal będziesz mieć lokalizację użytkownika na mapie. Ustawienia tej właściwości YES zasadniczo prosi widok mapy, aby rozpocząć aktualizację i pobieranie lokalizacji użytkownika, aby pokazać ją na mapie. Z MKMapView.h komentarzy:

// Set to YES to add the user location annotation to the map and start updating its location
 11
Author: Aviel Gross,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-15 19:54:39

Wersja Swift:

func removeAllAnnotations() {
    let annotations = mapView.annotations.filter {
        $0 !== self.mapView.userLocation
    }
    mapView.removeAnnotations(annotations)
}
 6
Author: Kosuke Ogawa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-02 04:07:02

Swift 3

if let annotations = self.mapView.annotations {
    self.mapView.removeAnnotations(annotations)
}
 4
Author: mckanet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-10 19:26:32

Swift 2.0 Proste i najlepsze:

mapView.removeAnnotations(mapView.annotations)
 2
Author: Maselko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-18 12:51:32