Así que tengo un MKMapView con todos mis pines añadidos, y el color del pin depende de si se establece un valor para ese pin. Cuando carga la aplicación por primera vez, se llama al viewForAnnotation
y los colores se configuran en consecuencia. Sin embargo, cuando actualizo los detalles del pin (como ubicación, título, etc ...) también actualizo el pinColour para encontrar que no se actualiza. Parece que viewForAnnotation
no se vuelve a llamar después del agregado inicial.Force MKMapView viewForAnnotation para actualizar
He leído muchas preguntas similares a este y puedo confirmar que mapView.delegate = self;
Aquí es mi viewForAnnotation
código:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MapAnnotation *)annotation
{
if([annotation class] == MKUserLocation.class)
return nil;
NSString *pinIdentifier = annotation.identifier; // This is a unique string for each pin and is getting populated every time!
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];
if(annotationView == nil)
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
else
annotationView.annotation = annotation; // Never had this line fire...
annotationView.canShowCallout = YES;
annotationView.animatesDrop = NO;
annotationView.enabled = YES;
annotationView.tag = annotation.counter;
if(annotation.pinColour == Stopped) // from enum
annotationView.pinColor = MKPinAnnotationColorRed;
else
annotationView.pinColor = MKPinAnnotationColorGreen;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:@selector(mapCalloutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
infoButton.tag = annotation.counter;
annotationView.rightCalloutAccessoryView = infoButton;
return annotationView;
}
Aquí es el código en la que agrego el pasador:
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latestPosition.latitude;
annotationCoord.longitude = latestPosition.longitude;
MapAnnotation *annotation = [[MapAnnotation alloc] init];
annotation.coordinate = annotationCoord;
annotation.identifier = theIdentifier;
annotation.title = theTitle;
annotation.subtitle = theSubtitle
annotation.pinColour = [self getPinColour];
annotation.counter = theCounter;
[theMapView addAnnotation:annotation];
Aquí está el código donde actualizo el pin (método diferente para agregar):
updatePin = true;
pinCounter = mapPin.counter;
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latestPosition.latitude;
annotationCoord.longitude = latestPosition.longitude;
[mapPin setCoordinate:annotationCoord];
mapPin.identifier = theIdentifier;
mapPin.subtitle = theSubtitle;
mapPin.pinColour = [self getPinColour];
No estoy seguro de lo que me falta. viewForAnnotation
obviamente está funcionando, ¡simplemente no se llama después del agregado inicial! ¡Si llamara a esta función, estoy 100% seguro de que funcionaría como lo hace el cambio de color si reinicio la aplicación!
EDITAR: Ah, y realmente no quiero empezar a eliminar anotaciones y volver a agregarlas. ¡Es lo que estoy haciendo a corto plazo de todos modos!
Ah, gracias. Realmente no quería hacerlo de esa manera, pero logré retener la vista del texto destacado, ¡así que ahora no puedes decirlo de todos modos! Voy a marcar esto como la respuesta ya que no sabía que esta era la única manera. –
No, no es la respuesta correcta, ver a continuación –
Esto no funciona. – Aggressor