Jak usunąć wszystkie adnotacje z MKMapView oprócz adnotacji o lokalizacji użytkownika?

Używam removeAnnotations, Aby usunąć moje adnotacje z mapView, ale to samo usuwa lokalizację użytkownika ann. Jak Mogę temu zapobiec lub jak przywrócić user ann do widoku?

NSArray *annotationsOnMap = mapView.annotations;
        [mapView removeAnnotations:annotationsOnMap];
Author: nielsbot, 2012-06-02

6 answers

Aktualizacja:

Kiedy próbowałem z iOS 9 SDK adnotacja użytkownika nie jest już usuwana. Możesz po prostu użyć

mapView.removeAnnotations(mapView.annotations)

Odpowiedź historyczna (dla aplikacji, które działają na iOS przed iOS 9):

Spróbuj tego:

NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
[ annotationsToRemove removeObject:mapView.userLocation ] ;
[ mapView removeAnnotations:annotationsToRemove ] ;

EDIT: Swift version

let annotationsToRemove = mapView.annotations.filter { $0 !== mapView.userLocation }
mapView.removeAnnotations( annotationsToRemove )
 84
Author: nielsbot,
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-09-23 18:30:26

Aby wyczyścić wszystkie adnotacje z mapy:

[self.mapView removeAnnotations:[self.mapView annotations]];

Aby usunąć określone adnotacje z Mapview

 for (id <MKAnnotation> annotation in self.mapView.annotations)
{
    if (![annotation isKindOfClass:[MKUserLocation class]])
    {
              [self.mapView removeAnnotation:annotation];   
    }

}
Mam nadzieję, że to ci pomoże.
 19
Author: Aswathy Bose,
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-19 04:04:04

Dla Swift można po prostu użyć jednowierszowej:

mapView.removeAnnotations(mapView.annotations)

Edit: jak wspomniał nielsbot, usunie również adnotację o lokalizacji użytkownika, chyba że ustawisz ją tak:

mapView.showsUserLocation = true
 7
Author: raspi,
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-06 20:36:32

Jeśli Twoja lokalizacja użytkownika jest rodzajem klasy MKUserLocation, Użyj isKindOfClass, aby uniknąć usuwania adnotacji o lokalizacji użytkownika.

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

}

W przeciwnym razie możesz ustawić flagę, aby rozpoznać rodzaj adnotacji w – mapView:viewForAnnotation:.

 3
Author: bradley,
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-06-03 03:13:08

Może jakiś NSPredicate filtr?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"className != %@", NSStringFromClass(MKUserLocation.class)];
NSArray *nonUserAnnotations = [self.mapView.annotations filteredArrayUsingPredicate:predicate];
[self.mapView removeAnnotations:nonUserAnnotations];

Life is always better with nspredicate filter

 0
Author: Laszlo,
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-07-14 20:24:37

Hi try this I got the solution from this code:

 NSMutableArray*listRemoveAnnotations = [[NSMutableArray alloc] init];
[Mapview removeAnnotations:listRemoveAnnotations];

 [listRemoveAnnotations release];
 -1
Author: madesh,
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-11-30 06:46:35