为iOS动态设置地图引脚颜色

时间:2012-11-23 01:46:06

标签: ios mapkit mkannotation mkannotationview

我解析包含字符串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;
    }

1 个答案:

答案 0 :(得分:3)

此代码位于viewForAnnotation

if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {

不会有两个原因:

  1. 它使用单个等号来进行赋值 - 而不是用于检查相等性。它需要使用两个相等的符号来检查是否相等。但是,这并不能解决主要问题,即理由#2 ...

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

  3. 相反,您必须使用传递给委托方法的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

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