Ste*_*fan 39 iphone cocoa-touch mapkit mkpinannotationview
根据Apple文档,MKPinAnnotationView的针脚颜色有红色,绿色和紫色可供选择.有没有办法得到其他颜色?我在文档中找不到任何内容.
yon*_*nel 81
多一点 ;)
alt text http://lionel.gueganton.free.fr/pins/pinGray.png

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

原来的:
alt text http://lionel.gueganton.free.fr/pins/pinGreen.png

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

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

和代码:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
Red*_*ing 40
您可能会发现以下图片很有用:

以及在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];
}
// ...
Run Code Online (Sandbox Code Playgroud)
我喜欢Yonel的答案,但只是抬头,当你创建一个自定义的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;
}
Run Code Online (Sandbox Code Playgroud)