且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多个注释数组,每个数组的引脚颜色不同?

更新时间:2023-01-26 23:31:03

您是否定义了一个实现 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 annotationTypeint 属性,那么您可以在 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:

  • 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.