0

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
user616076
  • 3,907
  • 8
  • 38
  • 64
  • See http://stackoverflow.com/questions/9814988/mkmapview-instead-of-annotation-pin-a-custom-view. Code is creating an MKPinAnnotationView instead of plain MKAnnotationView. –  Nov 26 '14 at 19:22
  • Thank you Anna that worked. I have another issue which is the following post, I hope you can help me again. – user616076 Nov 27 '14 at 10:55

2 Answers2

0

The answer provided by Anna above solved the problem

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

Community
  • 1
  • 1
user616076
  • 3,907
  • 8
  • 38
  • 64
0

The MKPinAnnotationView class provides a concrete annotation view that displays a pin icon like the ones found in the Maps application. Using this class, you can configure the type of pin to drop and whether you want the pin to be animated into place.This Class is inherited from MKAnnotationView but dont have image property itself.You can use the MKAnnotation​View class as is or subclass it to provide custom behavior as needed. The image property of the class lets you set the appearance of the annotation view without subclassing directly.

So instead of Using MKPinAnnotationView use MKAnnotationView. Check out this code for custom image rather than the default image of ios for pin annotations.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if (annotation == mapView.userLocation)
    {
        return nil;
    }
    else
    {
        MKAnnotationView *pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annot"];
        if (!pinView)
        {
            // if an existing pin view was not available, create one
            MKAnnotationView *customPinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annot"];
                customPinView.image = [UIImage imageNamed:@"yourImage.png"];

            return customPinView;
        }
        else
        {
            pinView.annotation = annotation;
        }

        return pinView;
    }
}
Yogesh Dalavi
  • 73
  • 4
  • 15