Search code examples
iosobjective-cannotationsmapkitkml

Changing my Pin to Custom Image not working in my MKMapView


I'm trying to change my pin images in my map view. The annotations are created from parsing a KML file as below:

-(void)loadMap
{
   NSURL *url = [NSURL fileURLWithPath:path];
   kmlParser = [[KMLParser alloc] initWithURL:url];
   [kmlParser parseKML];

   NSArray *overlays = [kmlParser overlays];
   [map addOverlays:overlays];

   NSArray *annotations = [kmlParser points];
   [map addAnnotations:annotations];
   MKMapRect goTo = MKMapRectNull;

   for (id <MKOverlay> overlay in overlays) {
       if (MKMapRectIsNull(goTo)) {
           goTo = [overlay boundingMapRect];
       } else {
           goTo = MKMapRectUnion(goTo, [overlay boundingMapRect]);
       }
   }

   for (id <MKAnnotation> annotation in annotations) {
       MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
       MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
       if (MKMapRectIsNull(goTo)) {
           goTo = pointRect;
       } else {
           goTo = MKMapRectUnion(goTo, pointRect);
       }
   }
   map.visibleMapRect = goTo;
}

The code then runs the viewForAnnotation method below which should change my pin image to my custom pin image.

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{  
     MKAnnotationView *pinView = [kmlParser viewForAnnotation:annotation];
     if(annotation != map.userLocation)
     {
        static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
        MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                    initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];

        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
        pinView.image = [UIImage imageNamed:@"bigPin.png"];
    }
    return pinView;
}

The viewForAnnotation method runs and it appears to change the image, but the map still shows the original pin image. What am I doing wrong?


Solution

  • The answer provided by Anna above solved the problem

    See stackoverflow.com/questions/9814988/…. Code is creating an MKPinAnnotationView instead of plain MKAnnotationView.