Multiple Arrays of Annotations, Different Pin Color for Each Array?

微笑、不失礼 提交于 2020-01-01 07:05:52

问题


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.

Thanks!


回答1:


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?

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:

  • 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 using view.annotation.



回答2:


I would suggest you to create a custom MKAnnotation and have a custom property (most propbably a typedef enum) to distinguish between the different types of annotations.

typedef enum {
    Food,
    Gas,
    Shopping
} AnnotationType

After that you can set your colors conditionally if (annotation.annotationType == Food) { set pinColor }

Of course you can use a switch statement for the AnnotationType for having clearer code:

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

See following question for more info on adding more colors (if you want to extend your app later on) :

MKPinAnnotationView: Are there more than three colors available?


Here's a code snippet from a tutorial that show's heavy modifications:

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



来源:https://stackoverflow.com/questions/7084089/multiple-arrays-of-annotations-different-pin-color-for-each-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!