¿Cómo cambiar el punto azul de la location del usuario de MKMapView a una image preferida?

¿Es posible cambiar el punto azul que indica la location del usuario en MKMapView a una image? Por ejemplo, un coche pequeño o cualquier image .png ?

introduzca la descripción de la imagen aquí

En el método viewForAnnotation: de MKMapViewDelegate probablemente tengas el código como este.

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { if (annotation == mapView.userLocation) return nil; ... 

Devuimos nil si la anotación es userLocation para permitir que mapView muestre la animation de punto y círculo azul. Para mostrar nuestra anotación personalizada para userLocation simplemente elimine la línea return nil; y hacer tu personalización allí.

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString* AnnotationIdentifier = @"Annotation"; MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier]; if (!pinView) { MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; if (annotation == mapView.userLocation){ customPinView.image = [UIImage imageNamed:@"myCarImage.png"]; } else{ customPinView.image = [UIImage imageNamed:@"mySomeOtherImage.png"]; } customPinView.animatesDrop = NO; customPinView.canShowCallout = YES; return customPinView; } else { pinView.annotation = annotation; } return pinView; } 

Aquí está la versión Swift 2.0 en la que puede tener varios pines.

En este código CustomAnnotation es solo una subclass MKAnnotation. Básicamente, si la anotación no es el tipo de una de tus classs personalizadas, entonces es el pin de la location del usuario.

 func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { // This is false if its a user pin if(annotation.isKindOfClass(CustomAnnotation) == false) { let userPin = "userLocation" if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(userPin) { return dequeuedView } else { let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: userPin) mkAnnotationView.image = UIImage(named: C_GPS.ROUTE_WALK_ICON_NAME) let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height / 2) mkAnnotationView.centerOffset = offset return mkAnnotationView } } let annotation = annotation as? CustomAnnotation if(annotation == nil) { return nil } let endPointsIdentifier = "endPoint" if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(endPointsIdentifier) { dequeuedView.image = annotation!.uiimage return dequeuedView } else { let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: endPointsIdentifier) mkAnnotationView.image = annotation!.uiimage let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height / 2) mkAnnotationView.centerOffset = offset let gesture = UITapGestureRecognizer(target: self, action: "routeTouched:") mkAnnotationView.addGestureRecognizer(gesture) return mkAnnotationView } } 

Ok, aquí está la versión Swift:

 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { let identifier = "User" var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) if annotationView == nil{ annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) annotationView.canShowCallout = true } else { annotationView.annotation = annotation } annotationView.image = UIImage(named: "image") return annotationView } 

¿Es esto para cambiar el punto azul de la location actual?

 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { let identifier = "User" var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) if annotationView == nil{ annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) annotationView.canShowCallout = true } else { annotationView.annotation = annotation } annotationView.image = UIImage(named: "image") return annotationView } 

Intenta esto de esta manera. Está funcionando para mí en Xcode 7 y Swift 2.

  func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { // want to show a custom image if the annotation is the user's location. guard !annotation.isKindOfClass(MKUserLocation) else { let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation") annotationView.image = UIImage(named: "icon_coordinates_self") return annotationView //return nil } // for other annotation except current location let annotationIdentifier = "AnnotationIdentifier" var annotationView: MKAnnotationView? if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) { annotationView = dequeuedAnnotationView annotationView?.annotation = annotation } else { let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) annotationView = av } if let annotationView = annotationView { // Configure your annotation view here annotationView.canShowCallout = true annotationView.image = UIImage(named: "Annotation_map") } return annotationView }