Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 200
jeff Avatar asked May 30 '11 16:05

jeff


2 Answers

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!

like image 113
nig Avatar answered Sep 22 '22 15:09

nig


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;
like image 34
canbeing Avatar answered Sep 21 '22 15:09

canbeing