【问题标题】:Multiple Arrays of Annotations, Different Pin Color for Each Array?多个注释数组,每个数组的引脚颜色不同?
【发布时间】:2011-08-16 19:48:13
【问题描述】:

我的应用中有三组注释: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;
}

如何设置它以对每个注释数组使用三种不同的引脚颜色。

谢谢!

【问题讨论】:

    标签: iphone ios annotations mkmapview


    【解决方案1】:

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

    假设您已经定义了一个注释类,其中您的注释类中有一个名为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;
    }
    


    其他几件事:

    【讨论】:

      【解决方案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 的代码 sn-p,显示了重大修改:

      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

      【讨论】:

      • 我有没有办法使用内置的 mkpinannotationview 和 pinview.pincolor=____ 来做到这一点?
      • 是的,您也可以使用MKAnnotationView 的 pinColor 属性来执行此操作。我也用更相关的链接更新了我的问题。
      • 非常感谢!我现在就去处理它!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多