Custom annotation image in iOS

18,756

Solution 1

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!

Solution 2

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;

Solution 3

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.

Solution 4

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

Share:
18,756
jeff
Author by

jeff

Updated on June 05, 2022

Comments

  • jeff
    jeff almost 2 years

    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?

  • SinisterMJ
    SinisterMJ almost 11 years
    PinAnnotationView is supposed to use use only the built-in pin images as I understand it, so I would not think it would support setting a custom image...