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

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

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

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;
    }

最佳答案

此代码在viewForAnnotation中:

if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {

不能工作有两个原因:
  • 它使用单个等号分配,而不是检查相等性。它需要使用两个等号来检查是否相等。但是,这不能解决主要问题,原因是原因2 ...
  • 该代码正在检查myAnnotation的值,该值是在此委托方法外部设置的变量。无法保证将与设置myAnnotation的for循环同步调用委托方法。不要假设在调用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的标题呢?)

    关于ios - 为iOS动态设置 map 图钉颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13522198/

    10-11 22:53
    查看更多