2011-08-16 116 views
2

我在我的app,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; 
} 

我怎样才能设置这一点,以便为每个注释数组使用三种不同的针脚颜色。

谢谢!

回答

2

我建议你创建一个自定义的MKAnnotation并有一个自定义属性(最可能是typedef枚举)来区分不同类型的注释。

typedef enum { 
    Food, 
    Gas, 
    Shopping 
} AnnotationType 

之后,你可以设置你的颜色有条件if (annotation.annotationType == Food) { set pinColor }

当然你也可以使用的AnnotationType switch语句具有更清晰的代码:

switch(annotation.annotationType) { 
    case Food: 
     do something; 
     break; 
    case Gas: 
     do something; 
     break; 
    case Shopping: 
     do something; 
     break; 
} 

更多信息,请参见以下问题上添加更多颜色(如果您想稍后扩展您的应用):

MKPinAnnotationView: Are there more than three colors available?


下面是一个tutorial,显示的沉重修改的代码片段:

calloutMapAnnotationView.contentHeight = 78.0f; 
UIImage *asynchronyLogo = [UIImage imageNamed:@"asynchrony-logo-small.png"]; 
UIImageView *asynchronyLogoView = [[[UIImageView alloc] initWithImage:asynchronyLogo] autorelease]; 
asynchronyLogoView.frame = CGRectMake(5, 2, asynchronyLogoView.frame.size.width, asynchronyLogoView.frame.size.height); 
[calloutMapAnnotationView.contentView addSubview:asynchronyLogoView]; 

HTH

+0

有没有办法,我可以做到这一点使用内置的mkpinannotationview和pinview.pincolor = ____? – Matt

+0

是的,你也可以使用'MKAnnotationView'的pinColor属性来做到这一点。此外,我更新了一个更相关的链接我的问题。 –

+0

非常感谢!我现在要开始工作了! – Matt

6

是否定义了实现在其中添加了一些属性标识MKAnnotation协议的自定义类它是什么样的注释?或者你是否定义了三个独立的类(每个类型的注释)实现MKAnnotation

假设你定义在你有一个int属性调用一个注解类说annotationType在你的注释类,那么您可以在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; 
} 


一对夫妇的其他东西: