3

I've written some code for showing annotations with custom images in a mapview. My mapview delegate implements this method to customize annotation when they are put in the map:

- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation {
if ([annotation isKindOfClass:[Station class]]) {
    Station *current = (Station *)annotation;
    MKPinAnnotationView *customPinview = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    if([[current type] compare:FONTANELLA]==NSOrderedSame)
        customPinview.pinColor = MKPinAnnotationColorPurple;
    else{
        int test=current.bici;
        if(test==0)
            customPinview.image = [UIImage imageNamed:@"bicimir.png"];
        else if(test<4)
            customPinview.image = [UIImage imageNamed:@"bicimi.png"];
        else if(test>=4)
            customPinview.image = [UIImage imageNamed:@"bicimig.png"];
    }
    customPinview.animatesDrop = NO;
    customPinview.canShowCallout = YES;
    return customPinview;
}
else{
    NSString *identifier=@"MyLocation";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    return annotationView;
}

}

The problem is a strange behavior when I long click on a custom annotation on the map: The image change and the default red pin is shown.

Why this behavior? And How can I avoid it?

Matteo
  • 276
  • 1
  • 4
  • 18

2 Answers2

3

When you want to use a custom image for an annotation view, create a generic MKAnnotationView instead of an MKPinAnnotationView.

The MKPinAnnotationView really likes to display its default image which is a pin.

Rearrange the logic a bit so that for FONTANELLA, it creates an MKPinAnnotationView but for the rest an MKAnnotationView.

Also, you should really implement annotation view re-use for all cases (and the last else part doesn't make sense since nothing is done if the dequeue doesn't return anything--you could just do return nil; instead).

0

inside .h file

@interface AddressAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *mPinColor;

}

@property (nonatomic, retain) NSString *mPinColor;

@end

in .m file

@implementation AddressAnnotation

@synthesize coordinate mPinColor;


- (NSString *)pincolor{
    return mPinColor;
}

- (void) setpincolor:(NSString*) String1{
    mPinColor = String1;
}


-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
    coordinate=c;
    NSLog(@"%f,%f",c.latitude,c.longitude);
    return self;
}
@end

inside .m class file

- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(AddressAnnotation *) annotation{

    UIImage *anImage=[[UIImage alloc] init];

MKAnnotationView *annView=(MKAnnotationView*)[mapView1 dequeueReusableAnnotationViewWithIdentifier:@"annotation"];

    if(annView==nil)
    {
        annView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"] autorelease];

    }

    if([annotation.mPinColor isEqualToString:@"green"])
    {

        anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google pin green.png" ofType:nil]];
    }
    else if([annotation.mPinColor isEqualToString:@"red"])
    {
        anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google pin red.png" ofType:nil]];
    }
    else if([annotation.mPinColor isEqualToString:@"blue"])
    {
        anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google pin blue.png" ofType:nil]];
    }

    annView.image = anImage;

    return annView;

}
Anil Kothari
  • 7,653
  • 4
  • 20
  • 25