MKPinAnnotationView:有三种以上的颜色吗?

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)

  • 我在模拟器中运行了一段代码,使用UIImagePNGRepresentation(yourImage)将UIImage写入文件系统.图像 - > NSData - > fileSystem.我没有@ 2x版本:/ (5认同)
  • 亚光,我为针脚添加了视网膜显示屏.以及用于渲染它们的代码. (2认同)

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)


Nic*_*ard 11

您可以使用以下ZSPinAnnotation命令动态创建注释引脚UIColor:https://github.com/nnhubbard/ZSPinAnnotation


Bad*_*ate 8

我喜欢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)