iosobjective-cmkmapviewmkmapviewdelegatemkpointannotation

Add an image to MKPointAnnotation without changing User Location image


I am using this code to add image to MKPointAnnotation.

    - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *SFAnnotationIdentifier = @"SFAnnotationIdentifier";
    MKPinAnnotationView *pinView =
    (MKPinAnnotationView *)[_routeMap dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
    if (!pinView)
    {
        MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                         reuseIdentifier:SFAnnotationIdentifier];
        UIImage *flagImage = [UIImage imageNamed:@"BikeIconMap"];
        // You may need to resize the image here.
        annotationView.image = flagImage;
        return annotationView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}

But it is showing the custom image for user location as well. The circular wave animation of user location is also gone.

enter image description here enter image description here

Is there a way to add image to MKPointAnnotation without changing the user location image and animation?


Solution

  • Add this code in the viewForAnnotation: method of MKMapViewDelegate

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