Search code examples
iphoneobjective-ccocoa-touchmkmapviewmkannotationview

MKAnnotationView custom image is not shown


I am trying to add a custom image instead of the regular pin on the map. But it remains a red pin... What am I missing?

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{  
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    annView.animatesDrop = TRUE;
    annView.image = [UIImage imageNamed:@"CustomPin.png"];
    return annView;
}

Solution

  • MKMapView: Instead of Annotation Pin, a custom view

    -(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
    {
        MKAnnotationView *pinView = nil; 
        if(annotation != mapView.userLocation) 
        {
            static NSString *defaultPinID = @"com.invasivecode.pin";
            pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
            if ( pinView == nil ) 
                pinView = [[MKAnnotationView alloc]
                                             initWithAnnotation:annotation reuseIdentifier:defaultPinID];
    
            //pinView.pinColor = MKPinAnnotationColorGreen; 
            pinView.canShowCallout = YES;
            //pinView.animatesDrop = YES;
            pinView.image = [UIImage imageNamed:@"pinks.jpg"];    //as suggested by Squatch
        } 
        else {
            [mapView.userLocation setTitle:@"I am here"];
        }
        return pinView;
    }