15 Jul 2009, 23:35
Generic-user-small

phawanna (3 posts)

I use Map Kit and I am trying to custom pin image with different icons in the “viewForAnnotation”. I got the custom pin image to show up on the map. However, the problem is when I click on the icon/pin, the icon is changed to red dot and not the custom image no more. What is the problem? Would you be able to give suggestion? Thank you,

Here is part of the code I have. `

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
            viewForAnnotation:(id <MKAnnotation>)annotation
{
.......
		UIImage *pinIconImg =[[UIImage imageNamed:@"imagename.png"] retain];
		/*set the pin color according to the distance*/
		if([resourcetype isEqualToString:@"F"]) {
			[(MKPinAnnotationView *)view setImage:pinIconImg];	
				
		} else if([resourcetype isEqualToString:@"N"]) {
			[(MKPinAnnotationView *)view setImage:pinIconImg];	
		} 
		else if([resourcetype isEqualToString:@"D"]) 
		{
			[(MKPinAnnotationView *)view setImage:pinIconImg];	
		}
		
		[view setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];
		[(MKPinAnnotationView *)view setAnimatesDrop:NO];
		[view setSelected:NO];
		[view setEnabled:YES];
		[view setCanShowCallout:YES];
.........
}

`

16 Jul 2009, 05:57
Generic-user-small

phawanna (3 posts)

It is ok. I found a solution.

24 Jul 2009, 20:26
John-davies-photo_pragsmall

John Davies (1 post)

Just in case anyone else is looking to customize their pins this is how to do it, well at least it worked for me…

@interface CustomPin : MKAnnotationView
{
}
- (id)initWithAnnotation:(id <MKAnnotation>) annotation;
@end

@implementation CustomPin

- (id)initWithAnnotation:(id <MKAnnotation>)annotation
{
    self = [super initWithAnnotation:annotation reuseIdentifier:@"CustomId"];

    if (self)		
    {
        UIImage*    theImage = [UIImage imageNamed:@"pin-image.png"];

        if (!theImage)
            return nil;
		
        self.image = theImage;
    }	
    return self;
}
@end

To use it simply call it in the mapView viewForAnnotation method…

if(annotation != mapView.userLocation) {
	MyAnnotation *annotation = (MyAnnotation*)annotation;
	view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomId"];
	if(nil == view) {
		view = [[[CustomPin alloc] initWithAnnotation:annotation] autorelease];
	}
	[view setCanShowCallout:YES];
	[view setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];
}

I actually extended the “initWithAnnotation” method to take another object so that I could select the image from several.

Another interesting thing I found (although it’s no doubt in the doc that I’ve never read) is that the centre of the pin image is where the map co-ordinate is, obvious perhaps but if you’re drawing pins rather than targets as I was then you need to create an image twice the size of the pin and have the pin occupy the top section of the image so that the centre of the image is at the bottom of the pin.

Good luck,

John Davies twitter:@jtdavies

06 Aug 2009, 15:36
Generic-user-small

Joey Schluchter (1 post)

I had been looking for an example of this everywhere! Thanks John!

  You must be logged in to comment