Selon les docs Apple, la couleur de la broche de MKPinAnnotationView est disponible en rouge, vert et violet. Est-il possible d'obtenir d'autres couleurs aussi? Je n'ai rien trouvé dans la documentation.
MKPinAnnotationView: Y at-il plus de trois couleurs disponibles?
un peu plus ;)
alt texte http://lionel.gueganton.free.fr/pins/pinGray.png

alt texte http://lionel.gueganton.free.fr/pins/pinOrange.png

Et ceux d'origine:
alt texte http://lionel.gueganton.free.fr/pins/pinGreen.png

alt texte http://lionel.gueganton.free.fr/pins/pinPurple.png

alt texte http://lionel.gueganton.free.fr/pins/pinRed.png

Et le 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;
}
Vous trouverez peut-être les images suivantes utiles:

et le code pour les utiliser dans 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];
}
// ...
Vous pouvez utiliser ZSPinAnnotationpour créer des broches d'annotation à la volée avec un spécifié UIColor: https://github.com/nnhubbard/ZSPinAnnotation
J'aime la réponse de Yonel mais seulement une tête, lorsque vous créez une coutume MKAnnotationView, vous devrez attribuer manuellement le décalage. Pour les images Yonel fournies: (vous pouvez laisser les trucs de calloutButton si vous n'avez pas besoin l' un de ceux -ci )
#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;
}
Avec iOS 9, pinTintColora été ajouté à MKPinAnnotationViewvous permettre de fournir une UIColorpour la couleur de la broche.
Et voici le PSD pour la broche avec l'ombre et son en @ 2x taille.
http://dl.dropbox.com/u/5622711/ios-pin.psd
Utilisez cette PSD pour une couleur que vous voulez :)
Je ne prends aucun crédit pour cette PSD. Je viens attrapée de http://www.teehanlax.com/downloads/iphone-4-guid-psd-retina-display/ Ils ont fait un travail merveilleux!
Aucune des solutions affichées fonctionnent 100% si vous utilisez l'animation de chute de la broche. La solution de canonnade est très soignée car elle permet la broche d'avoir encore les deux types d'extrémités (la pointe acérée quand la chute et l'un avec l'ondulation du papier circulaire) mais malheureusement un aperçu de la couleur de la tête de la broche originale peut être vu lorsque les rebonds des broches comme il frappe la carte. La solution de Yonel de remplacer toute l'image de la broche signifie la broche tombe avec l'ondulation du papier circulaire avant qu'il ne soit même frappé la carte!
J'ai essayé de cette façon et il semble être ok ...
UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:image]
autorelease];
[annotationView addSubview:imageView];
annotationView = nil;
en utilisant l'image de la broche complète ... comme l'exemple yonel
Si ce n'est pas dans les documents plus que probablement pas, vous pouvez utiliser mkannotationview et ont ur propre image si u veulent bien













