jeff
jeff

Reputation: 73

Custom annotation image in iOS

I am trying to replace the typical iOS mapkit "pin" with a particular image. I'm new to coding so I'm not sure exactly why this isn't working, but here is what i've attempted:

for the method

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

i have, among other things, this:

pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
//pinView.pinColor=MKPinAnnotationColorPurple;
UIImage *pinImage = [UIImage imageNamed:@"Jeff.png"];
[pinView setImage:pinImage];

However, even though the code compiles fine, the pinView is not being set the pinImage. Any ideas why not?

Upvotes: 6

Views: 18795

Answers (5)

alex_newbie
alex_newbie

Reputation: 11

Just delete "animatesDrop" line from your code. It will work for you.

Upvotes: 1

The answer I am not seeing here, is that you must dequeue or create an MKAnnotationView within the viewForAnnotation method, and also have your class as a delegate of the map view so that it calls this method to get the view.

If you have created the annotation views elsewhere any custom images set using the image property will not display, and you will only see a pin.

Upvotes: 2

Bedevere
Bedevere

Reputation: 47

Just do:

annView.image=[UIImage imageNamed:@"imagename.png"];

Upvotes: 0

canbeing
canbeing

Reputation: 31

the image should set to the button instand of the annView, I use like this:

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateNormal];
[button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateSelected];
[button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateHighlighted];
pinView.rightCalloutAccessoryView = button;

Upvotes: 3

nig
nig

Reputation: 39

all you have to do is change the last line to:

[pinView addSubview:pinImage];

full example:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ParkingPin"];

    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin_parking.png"]] autorelease];

    annView.animatesDrop = TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    [annView addSubview:imageView];
    return annView;
}

hope this helps!

Upvotes: 3

Related Questions