多个注释数组,每个数组的引脚颜色不同? [英] Multiple Arrays of Annotations, Different Pin Color for Each Array?
问题描述
我的应用中有三个注释数组,foodannotations、gasannotations 和shoppingannotations.我希望每个注释数组都显示不同颜色的图钉.我目前正在使用
I have three arrays of annotations in my app, foodannotations, gasannotations, and shoppingannotations. I want each array of annotations to show a different colored pin. I am currently using
- (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;
}
如何将其设置为对每个注释数组使用三种不同的引脚颜色.
How can I set this up to use the three different pin colors for each array of annotations.
谢谢!
推荐答案
您是否定义了一个实现 MKAnnotation
协议的自定义类,您在其中添加了一些属性来标识它是哪种注释?或者您是否定义了三个单独的类(每种类型的注释一个)来实现 MKAnnotation
?
Have you defined a custom class that implements the MKAnnotation
protocol in which you added some property that identifies what kind of annotation it is? Or have you defined three separate classes (one for each type of annotation) that implement MKAnnotation
?
假设您已经定义了一个注释类,其中您的注释类中有一个名为 say annotationType
的 int
属性,那么您可以在 viewForAnnotation 中执行此操作代码>:
Assuming you've defined one annotation class in which you have an int
property called say annotationType
in your annotation class, then you can do this in 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;
}
其他一些事情:
A couple of other things:
- 您应该使用
dequeueReusableAnnotationViewWithIdentifier:
方法 - 不要使用 addTarget:action 和自定义方法来处理标注按钮按下,而是使用地图视图的委托方法
calloutAccessoryControlTapped
.在该委托方法中,您可以使用view.annotation
访问注释.
- You should use the
dequeueReusableAnnotationViewWithIdentifier:
method - Instead of using addTarget:action and a custom method to handle the callout button press, use the map view's delegate method
calloutAccessoryControlTapped
. In that delegate method, you can access the annotation usingview.annotation
.
这篇关于多个注释数组,每个数组的引脚颜色不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!