39

According to the Apple docs, MKPinAnnotationView's pin color is available in red, green and purple. Is there any way to get other colors also? I've found nothing in the docs.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
Stefan
  • 28,843
  • 15
  • 64
  • 76
  • 4
    You could use ZSPinAnnotation to create annotation pins on the fly with a specified UIColor: https://github.com/nnhubbard/ZSPinAnnotation – Nic Hubbard Mar 09 '12 at 04:10

9 Answers9

81

some more ;)

alt text enter image description hereenter image description here

alt text enter image description hereenter image description here

And the original ones :

alt text alt text enter image description here

alt text alt text enter image description here

alt text alt text enter image description here

And the code:

- (MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView* anView =[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"test"];
anView.pinColor=MKPinAnnotationColorPurple;
UIImage* image = nil;
// 2.0 is for retina. Use 3.0 for iPhone6+, 1.0 for "classic" res.
UIGraphicsBeginImageContextWithOptions(anView.frame.size, NO, 2.0);
[anView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData* imgData = UIImagePNGRepresentation(image);
NSString* targetPath = [NSString stringWithFormat:@"%@/%@", [self writablePath], @"thisismypin.png" ];
[imgData writeToFile:targetPath atomically:YES]; 
return anView;
}

-(NSString*) writablePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
yonel
  • 7,855
  • 2
  • 44
  • 51
40

You might find the following images useful:

alt text alt text alt text alt text

and the code to use them in viewForAnnotation:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{   
    // ... get the annotation delegate and allocate the MKAnnotationView (annView)
    if ([annotationDelegate.type localizedCaseInsensitiveCompare:@"NeedsBluePin"] == NSOrderedSame)
    {
        UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
        [annView addSubview:imageView];
    }
    // ...
Community
  • 1
  • 1
RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
11

You could use ZSPinAnnotation to create annotation pins on the fly with a specified UIColor: https://github.com/nnhubbard/ZSPinAnnotation

Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412
  • Hey @Nic-Hubbard - thanks for sharing the code. Is the reason you wouldn't add this as a category on MKAnnotation to let you set `pinView.image = [MKAnnotation pinAnnotationWithColor:a.color]` – earnshavian Mar 08 '12 at 19:41
  • 1
    @earnshavian No reason really, I guess I didn't think about it at the time! – Nic Hubbard Mar 09 '12 at 03:08
8

I like Yonel's Answer but just a heads up, when you create a custom MKAnnotationView, you'll have to manually assign the offset. For the images Yonel provided: (you can leave out the calloutButton stuff if you don't need one of those)

#pragma mark MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if(![annotation isKindOfClass:[MyAnnotation class]]) // Don't mess user location
        return nil;

    MKAnnotationView *annotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:@"spot"];
    if(!annotationView)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"spot"];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [(UIButton *)annotationView.rightCalloutAccessoryView addTarget:self action:@selector(openSpot:) forControlEvents:UIControlEventTouchUpInside];
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.centerOffset = CGPointMake(7,-15);
        annotationView.calloutOffset = CGPointMake(-8,0);
    }

    // Setup annotation view
    annotationView.image = [UIImage imageNamed:@"pinYellow.png"]; // Or whatever

    return annotationView;
}
Community
  • 1
  • 1
BadPirate
  • 25,802
  • 10
  • 92
  • 123
  • By the way, if the `annotationView` successfully dequeues the annotation view, you need to update its `annotation` property. E.g., you'd generally have a `if (!annotationView) { ... } else { annotationView.annotation = annotation; }`. – Rob Aug 21 '17 at 00:17
4

With iOS 9, pinTintColor has been added to MKPinAnnotationView, allowing you to supply a UIColor for the pin color.

duncanc4
  • 1,191
  • 1
  • 9
  • 17
4

And here is the PSD for the pin with shadow and its in @2x size.

http://dl.dropbox.com/u/5622711/ios-pin.psd

Use this PSD for any color you want :)

I take no credit for this PSD. I just grabbed it from http://www.teehanlax.com/downloads/iphone-4-guid-psd-retina-display/ They have done a wonderful job!

Vashishtha Jogi
  • 12,120
  • 2
  • 19
  • 20
3

Neither of the posted solutions work 100% if you are using the pin drop animation. Cannonade's solution is very neat because it allows the pin to still have both kinds of ends (the sharp point when falling and the one with the circular paper ripple) but unfortunately a glimpse of the original pin head colour can be seen when the pin bounces as it hits the map. yonel's solution of replacing the whole pin image means the pin falls with the circular paper ripple before it's even hit the map!

malhal
  • 26,330
  • 7
  • 115
  • 133
2

I tried this way and it seems to be ok...

UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:image]
                                 autorelease];
        [annotationView addSubview:imageView];
        annotationView = nil;

using the complete pin image... as the yonel example

mt81
  • 3,288
  • 1
  • 26
  • 35
1

If it's not in the docs then most probably not, you cAn use mkannotationview and have ur own image if u wish though

Daniel
  • 22,363
  • 9
  • 64
  • 71