Pergunta

Eu gostaria de remover todas as anotações do meu mapview sem o ponto azul da minha posição.Quando eu chamo:

[mapView removeAnnotations:mapView.annotations];

todas as anotações são removidos.

De que forma se pode verificar (como um loop em todas as anotações) se a anotação não é o ponto azul anotação?

EDITAR (Eu já resolveu com isso):

for (int i =0; i < [mapView.annotations count]; i++) { 
    if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotationClass class]]) {                      
         [mapView removeAnnotation:[mapView.annotations objectAtIndex:i]]; 
       } 
    }
Foi útil?

Solução

Olhando para o Documentação do MKMapView, parece que você tem a propriedade das anotações para brincar. Deve ser bastante simples iterar com isso e ver quais anotações você tem:

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

Você também tem o userLocation Propriedade que fornece a anotação que representa a localização do usuário. Se você passar pelas anotações e lembrar de todos eles que não são o local do usuário, poderá removê -los usando o removeAnnotations: método:

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];

Espero que isto ajude,

Sam

Outras dicas

Se você gosta de rápido e simples, há uma maneira de filtrar uma matriz da anotação MkUserLocation. Você pode passar isso para as notações de remoção do MKMapView: função.

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

Suponho que isso seja praticamente o mesmo que os filtros manuais publicados acima, exceto usando um predicado para fazer o trabalho sujo.

Não é mais fácil basta fazer o seguinte:

//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];

mais curta maneira de limpar todas as anotações e preservar a anotação da classe de mkuserlocation

[self.mapView removeAnnotations:self.mapView.annotations];
for (id annotation in map.annotations) {
    NSLog(@"annotation %@", annotation);

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

        [map removeAnnotation:annotation];
    }
    }

eu modificados, como este

É mais fácil fazer o seguinte:

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];

Para Swift 3.0

for annotation in self.mapView.annotations {
    if let _ = annotation as? MKUserLocation {
       // keep the user location
    } else {
       self.mapView.removeAnnotation(annotation)
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top