iphone - 多个注释数组,每个数组的引脚颜色不同?

标签 iphone ios annotations mkmapview

我的应用程序中有三个注释数组:foodannotations、gasannotations 和 shoppingannotations。我希望每个注释数组显示不同颜色的图钉。我目前正在使用

- (MKAnnotationView *)mapView:(MKMapView *)sheratonmap viewForAnnotation:(id<MKAnnotation>)annotation {
    NSLog(@"Welcome to the Map View Annotation");

    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString* AnnotationIdentifier = @"Annotation Identifier";
    MKPinAnnotationView* pinview = [[[MKPinAnnotationView alloc]
                                     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];

    pinview.animatesDrop=YES;
    pinview.canShowCallout=YES;
    pinview.pinColor=MKPinAnnotationColorPurple;

    UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightbutton setTitle:annotation.title forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(showDetails) forControlEvents:UIControlEventTouchUpInside];
    pinview.rightCalloutAccessoryView = rightbutton;

    return pinview;
}

我如何设置它以便为每个注释数组使用三种不同的图钉颜色。

谢谢!

最佳答案

您是否定义了一个自定义类来实现 MKAnnotation 协议(protocol),您在其中添加了一些属性来标识它是哪种注释?或者您是否定义了实现 MKAnnotation 的三个单独的类(一个用于每种注释类型)?

假设您已经定义了一个注释类,其中您在注释类中有一个名为 say annotationTypeint 属性,那么您可以在 viewForAnnotation 中执行此操作:

int annType = ((YourAnnotationClass *)annotation).annotationType;
switch (annType)
{
    case 0 :   //Food
        pinview.pinColor = MKPinAnnotationColorRed;
    case 1 :   //Gas
        pinview.pinColor = MKPinAnnotationColorGreen;
    default :  //default or Shopping
        pinview.pinColor = MKPinAnnotationColorPurple;
}


其他一些事情:

关于iphone - 多个注释数组,每个数组的引脚颜色不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7084089/

相关文章:

iphone - UINavigationBar 背景图像通过drawRect - 提示动画问题

ios - 在iOS 5上启动时应用崩溃

ios - 如何实现 snapchat 登录外观?

javascript - 如何在 API 17 下的构建中忽略 @JavascriptInterface 注释 -Android

iphone - 让 iPhone 振动 : AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) undeclared

iphone - MPMoviePlayerViewController 不播放下载的文件

iphone - 在 Interface Builder 的 iPad xib 中显式使用 @2x 图像

objective-c - 未发送/接收 NSNotificationCenter 通知

php - ParamConverter 可选但有效

java - 静态内部类需要导入注解