为iOS动态设置地图图钉颜色 [英] Setting Map Pin colour dynamically for iOS

查看:98
本文介绍了为iOS动态设置地图图钉颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I parse an xml that contains the string 0 ,1 and 2.

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

//For reference 0 = Green 1 = Red 2 = Purple

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

I have a class that confirms to the MKAnnotaion that contains the below variables that are properties.

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

此类名为MyAnnotation

This class is named MyAnnotation

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

Now in the viewDidLoad of the map view I run a for loop to iterate over the parsed data like the below (locationArray holds this data and I pull out all the info just fine.

 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保留为字符串

//This is the string that pulls out the mentioned 0, 1 or 2 strings which represent the colour of the pins poinType is retained as a string

    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视图中,我将执行以下操作

Now in the MKAnnotationView view i do the below

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

我也尝试了上述方法,但是它没有设置图钉颜色.其他一切都很好!

I have also tried in the above method however it is not setting the pin colours. Everything else is fine!

    //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) {

由于以下两个原因而无法正常工作:

will not work for two reasons:

  1. 它使用单个等号进行分配-而不是用于检查是否相等.它需要使用两个等号来检查是否相等.但是,这不能解决主要问题,原因是原因2 ...

  1. It is using a single equals sign which is for assignment -- not for checking equality. It needs to use two equal signs to check for equality. However, this doesn't fix the main issue which is reason #2...

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

The code is checking the value of myAnnotation which is a variable set outside this delegate method. There is no guarantee that the delegate method will be called in sync with the for-loop in which myAnnotation is set. Do not assume that viewForAnnotation will be called right after you call addAnnotation. It is even possible for the delegate method to be called multiple times for the same annotation if the map view needs to display the annotation again after a zoom or pan.

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

Instead, you must use the annotation parameter that is passed to the delegate method. This is a reference to the annotation the map view wants a view for in the current call of the delegate method.

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

Since the annotation parameter is typed generically as id<MKAnnotation>, you'll first have to cast it to your custom class and then you can access any custom properties:

//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的标题,即使它不可见?)

(Unrelated, but why is the code setting the title of rightButton even though it won't be visible?)

这篇关于为iOS动态设置地图图钉颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆