Apple docs によると、MKPinAnnotationView のピンの色は、赤、緑、紫で利用できます。他の色も入手する方法はありますか?ドキュメントには何も見つかりませんでした。
9 に答える
もう少し;)
そしてオリジナルのもの:
そしてコード:
- (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;
}
次の画像が役立つ場合があります。
そしてそれらを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];
}
// ...
ZSPinAnnotation
指定したものを使用して、オンザフライで注釈ピンを作成するために使用できますUIColor
: https://github.com/nnhubbard/ZSPinAnnotation
私はYonel's Answerが好きですが、カスタム MKAnnotationView を作成するときは、手動でオフセットを割り当てる必要があります。Yonel が提供した画像の場合: (calloutButton のいずれかが必要ない場合は、省略できます)
#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;
}
iOS 9 では、pinTintColor
が に追加され、ピンの色にMKPinAnnotationView
を指定できるようになりました。UIColor
そして、これは影付きのピンの PSD で、@2x サイズです。
http://dl.dropbox.com/u/5622711/ios-pin.psd
この PSD を任意の色に使用してください :)
私はこの PSD を信用しません。http://www.teehanlax.com/downloads/iphone-4-guid-psd-retina-display/から入手しました。素晴らしい仕事をしてくれました。
ピン ドロップ アニメーションを使用している場合、投稿されたソリューションはいずれも 100% 機能しません。キャノネードのソリューションは、ピンが両方の種類の端 (落下時の鋭いポイントと円形の紙の波紋のあるポイント) を持つことができるため、非常に優れていますが、残念ながら、ピンが跳ね返ったときに元のピンの頭の色を垣間見ることができます。マップにヒットします。ピン画像全体を置き換えるyonelのソリューションは、ピンがマップに当たる前に円形の紙の波紋とともに落ちることを意味します!
この方法で試してみましたが、大丈夫そうです...
UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:image]
autorelease];
[annotationView addSubview:imageView];
annotationView = nil;
完全なピン画像を使用して... yonelの例として
ドキュメントにない場合は、おそらくそうではありません。mkannotationviewを使用して、必要に応じて独自の画像を作成することもできます。