2

我解析了一个包含字符串 0 ,1 和 2 的 xml。

//供参考 0 = 绿色 1 = 红色 2 = 紫色

我有一个向 MKAnotaion 确认的类,其中包含以下作为属性的变量。

CLLocationCoordinate2D  coordinate;
NSString            *title;
NSString            *subtitle;
MKPinAnnotationColor pinColor;

这个类被命名为 MyAnnotation

现在在地图视图的 viewDidLoad 中,我运行一个 for 循环来迭代解析的数据,如下所示(locationArray 保存了这些数据,我可以很好地提取所有信息。

 for (int i = 0; i < locationArray.count; i++) {
    myAnnotation =[[MyAnnotation alloc] init];

    NSString *thePointName = [[locationArray objectAtIndex:i]xmlName];
    NSString *theAddress = [[locationArray objectAtIndex:i]xmlAddress];

    NSString *latString = [[locationArray objectAtIndex:i]xmlLat];
    NSString *lonString = [[locationArray objectAtIndex:i]xmlLon];

//这是拉出提到的0、1或2个字符串的字符串,这些字符串代表引脚的颜色 poinType被保留为字符串

    pointType = [[locationArray objectAtIndex:i]xmlType];

    double theLatitude = [latString doubleValue];
    double theLongtitude = [lonString doubleValue];

    userLocation.latitude=theLatitude;
    userLocation.longitude=theLongtitude;

    myAnnotation.coordinate=userLocation;
    myAnnotation.title=[NSString stringWithFormat:@"%@", thePointName];
    myAnnotation.subtitle=[NSString stringWithFormat:@"%@", theAddress];

    //I log that the points are actually giving either of the colors and if so set MyAnnotation class to the pincolor 

    NSLog(@"Points Color %@", pointType);
    if ([pointType isEqualToString:@"0"]){
        myAnnotation.pinColor = MKPinAnnotationColorGreen;
    }else if ([pointType isEqualToString:@"1"]){
        myAnnotation.pinColor = MKPinAnnotationColorRed;
    }else if ([pointType isEqualToString:@"2"]) {
        myAnnotation.pinColor = MKPinAnnotationColorPurple;
    }

    [mapView addAnnotation:myAnnotation];

    }

现在在 MKAnnotationView 视图中,我执行以下操作

  - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id     <MKAnnotation>)annotation

 {

// if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                  initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;

    //set pin color to the correct colour
    if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {
    pinView.pinColor = MKPinAnnotationColorGreen;
    }

    else if (myAnnotation.pinColor = MKPinAnnotationColorRed) {
    pinView.pinColor = MKPinAnnotationColorRed;

     }

    else if (myAnnotation.pinColor = MKPinAnnotationColorPurple){
    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;


    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"Profile.png"]];
pinView.leftCalloutAccessoryView = profileIconView;

return pinView;
  }

我也尝试过上述方法,但它没有设置引脚颜色。其他一切都很好!

    //set pin color to the correct colour
    if (pointType isEqualToString:@"0") {
    pinView.pinColor = MKPinAnnotationColorGreen;
    }

    else if (pointType isEqualToString:@"1") {
    pinView.pinColor = MKPinAnnotationColorRed;

    }

    else if (pointType isEqualToString:@"2"){
    pinView.pinColor = MKPinAnnotationColorPurple;
    }
4

1 回答 1

3

此代码在viewForAnnotation

if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {

不起作用有两个原因:

  1. 它使用一个用于赋值的等号——而不是用于检查相等性。它需要使用两个等号来检查是否相等。但是,这并不能解决原因#2的主要问题......

  2. 代码正在检查myAnnotationwhich 的值是在此委托方法之外设置的变量。不能保证委托方法将与设置的 for 循环同步调用myAnnotation。不要假设viewForAnnotation在您调用后会立即调用它addAnnotation。如果地图视图需要在缩放或平移后再次显示注释,甚至可以为同一个注释多次调用委托方法。

相反,您必须使用annotation传递给委托方法的参数。这是对地图视图在委托方法的当前调用中想要查看的注释的引用。

由于annotation参数的类型通常为id<MKAnnotation>,因此您首先必须将其强制转换为您的自定义类,然后您可以访问任何自定义属性:

//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
    MyAnnotation *currentAnn = (MyAnnotation *)annotation;

    if (currentAnn.pinColor == MKPinAnnotationColorGreen) {
        pinView.pinColor = MKPinAnnotationColorGreen;
    }

    else if (currentAnn.pinColor == MKPinAnnotationColorRed) {
        pinView.pinColor = MKPinAnnotationColorRed;

    }

    else if (currentAnn.pinColor == MKPinAnnotationColorPurple) {
        pinView.pinColor = MKPinAnnotationColorPurple;
    }
}

甚至更简单:

//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
    MyAnnotation *currentAnn = (MyAnnotation *)annotation;        
    pinView.pinColor = currentAnn.pinColor;
}

(不相关,但为什么代码设置的标题rightButton即使它不可见?)

于 2012-11-23T03:33:19.353 回答