Cómo eliminar todas las anotaciones en una MKMapView


¿Hay una manera sencilla de eliminar todas las anotaciones en un mapa sin iterar a través de todas las anotaciones mostradas en Objective-c?

Author: kevin Mendoza, 2010-06-12

8 answers

Sí, así es como

[mapView removeAnnotations:mapView.annotations]

Sin embargo, la línea de código anterior eliminará todas las anotaciones del mapa "PINES" de el mapa, incluyendo el pin de ubicación del usuario "Pin azul". Para eliminar todos los mapas anotaciones y mantener el pin de ubicación del usuario en el mapa, hay dos formas posibles de hacerlo

Ejemplo 1, conservar la anotación de ubicación del usuario, eliminar todos los pines, añadir la ubicación del usuario se fija de nuevo, pero hay un defecto con este enfoque, se hará que el pin de ubicación del usuario parpadear en el mapa, debido a la eliminación el pin y luego agregarlo

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

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

Ejemplo 2, personalmente prefiero evitar eliminar el pin de usuario de ubicación en primer lugar,

- (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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-25 09:53:48

Aquí está la forma más sencilla de hacerlo:

-(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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-15 10:59:07

He aquí cómo eliminar todas las anotaciones excepto la ubicación del usuario, escritas explícitamente porque imagino que volveré a buscar esta respuesta:

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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-29 07:02:00

Esto es muy similar a la respuesta de Sandip, excepto que no vuelve a agregar la ubicación del usuario para que el punto azul no parpadee de nuevo.

-(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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-20 23:14:08

No es necesario guardar ninguna referencia a la ubicación del usuario. Todo lo que se necesita es:

[mapView removeAnnotations:mapView.annotations]; 

Y mientras tenga mapView.showsUserLocation establecido en YES, todavía tendrá la ubicación del usuario en el mapa. Establece esta propiedad en YES básicamente le pide a la vista de mapa que comience a actualizar y recuperar la ubicación del usuario, para mostrarla en el mapa. De los MKMapView.h comentarios:

// 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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-15 19:54:39

Versión 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/ajaxhispano.com/template/agent.layouts/content.php on line 61
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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-10 19:26:32

Swift 2.0 Simple y lo mejor:

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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-18 12:51:32