2012-10-12 133 views
6

que añadir anotaciones a mi mapa de esta manera:El cambio de color pin MKMapView

MyAnnotation *annotationPoint2 = [[MyAnnotation alloc] init]; 
annotationPoint2.coordinate = anyLocation; 
annotationPoint2.title = [NSString stringWithFormat:@"%@", obj]; 
annotationPoint2.subtitle = @""; //or set to nil 
annotationPoint2.keyValue = [NSString stringWithFormat:@"%@", key]; 
[mapPins addAnnotation:annotationPoint2]; 

Los pines son todos de color rojo, y me gustaría que todos los verdes. ¿Cómo puedo cambiar el color? He intentado el siguiente, pero todavía da una marca roja:

annotationPoint2.pinColor = MKPinAnnotationColorGreen; 

Respuesta

18
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
    { 
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"]; 
    annView.pinColor = MKPinAnnotationColorGreen; 
    return annView; 
    } 
+0

este código, pero incluso la ubicación del usuario actual se convierte en verde, incluso si lo quiero azul con los círculos a su alrededor. ¿Cómo puedo hacer eso? – Alessandro

+4

if ([[título de la anotación] isEqualToString: @ "Ubicación actual"]) { annView.pinColor = MKPinAnnotationColorGreen; } else {annView.pinColor = MKPinAnnotationColorRed;} – casillas

+0

@Alessandro Debe devolver nulo cuando la anotación == mapView.userLocation para mostrar el punto azul para la ubicación del usuario y el círculo que lo rodea – amitshinik

5

pinColor La propiedad se define en la clase MKPinAnnotationView (no el protocolo MKAnnotation).

Crea un MKPinAnnotationView en el método delegado viewForAnnotation. Si no has implementado ese delegado, obtienes pins rojos estándar por defecto.

En ese método delegado, crea una instancia de MKPinAnnotationView y puede establecer su pinColor en verde.

1

Swift3 es de esta manera: trabaja

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

    let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin") 

    if annotation.title! == "My Place" { 

     annotationView.pinTintColor = UIColor.green 

    } else { 

     annotationView.pinTintColor = UIColor.red 
    } 


    return annotationView 
}