我的应用程序中有三个注释数组: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 annotationType
的 int
属性,那么您可以在 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;
}
其他一些事情:
- 你应该使用
dequeueReusableAnnotationViewWithIdentifier:
方法 - 不使用 addTarget:action 和自定义方法来处理标注按钮按下,而是使用 map View 的委托(delegate)方法
calloutAccessoryControlTapped
.在该委托(delegate)方法中,您可以使用view.annotation
访问注释。
关于iphone - 多个注释数组,每个数组的引脚颜色不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7084089/