仅针对MKMapView上的第一个和最后一个MKAnnotation设置MKPinAnnotationColorRed [英] Set MKPinAnnotationColorRed just for the first and last MKAnnotation on MKMapView

查看:127
本文介绍了仅针对MKMapView上的第一个和最后一个MKAnnotation设置MKPinAnnotationColorRed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义MKAnnotationNSArray,我需要为第一个/最后一个注释设置红色的图钉颜色,否则设置一个绿色的图钉. 该程序的行为不符合我的期望,绿色的针脚每次都以随机的方式与两个不同的注释相关联,而不与我想要的第一个和最后一个相关联.

I've an NSArray of custom MKAnnotation, i need to set a red pin color for the first/last annotation, otherwise set a green pin. The program does not behave as i want, the green pins are associated each time with two different annotations in a random way, are not associated with the first and the last as i want.

所以,这就是我在控制器中所做的:

So, this is what i'm doing in my controller:

- (void)viewDidLoad
{   
    [super viewDidLoad];   
    //load set a coords from file etc...  
    //self.coordinates is the array of annotations   
    [self.myMap addAnnotations:self.coordinates];  
} 

然后在viewForAnnotation中:回调:

then in the viewForAnnotation: callback:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    NSString *ident = @"MY_IDENTIFIER";
    MKPinAnnotationView *annView=(MKPinAnnotationView *)[self.myMap dequeueReusableAnnotationViewWithIdentifier:ident];
    if(self.myMap.userLocation==annotation){
        return nil;
    }
    if(annView == nil){
        annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ident];
        annView.animatesDrop=TRUE;
        annView.canShowCallout = YES;
        annView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annView.calloutOffset = CGPointMake(-5, 5);
        int currIdxAnn=[self.myMap.annotations indexOfObject:annotation];
        int lastIdxAnn=[self.myMap.annotations indexOfObject:[self.myMap.annotations lastObject]];
        /*
        if (currIdxAnn==0 || currIdxAnn==lastIdxAnn) {
            annView.pinColor=MKPinAnnotationColorRed;
        }else {
            annView.pinColor=MKPinAnnotationColorGreen;
        }*/
         CustomAnnotation *an=(CustomAnnotation *)annotation;
         if (an.tag==98||an.tag==99) {
            annView.pinColor=MKPinAnnotationColorGreen;
         }else {
            annView.pinColor=MKPinAnnotationColorRed;
         }

    }
    return annView;
}

似乎方法addAnnotations:不能像我所相信的那样工作,可能以与数组不同的顺序加载注释.有可能吗?
我也尝试在didAddAnnotationViews:中进行类似的过程,但效果不佳.

It seems that the method addAnnotations: does not work as I believe, probably loads the annotations with an order different from the array. Is it possible?
I also tried to make a similar process in didAddAnnotationViews: but without good results.

一些提示? 谢谢.

PS:当我写完这个问题时,我发现了响应,似乎证实了我的理论.有人已经遇到过类似的问题吗?

PS:as I finished writing this question i found this response, and seems to confirm my theory. Someone has already encountered a similar problem?

编辑:经过几次测试,我意识到,完成我想要的操作的最佳方法是首先在我的第一个/最后一个注释中设置标签:

EDIT: After several tests i realized that the best way to accomplish what i want is to first set a tag to my first/last annotations:

CustomAnnotation *first=[self.coordinates objectAtIndex:0];
first.tag=98;
CustomAnnotation *last=[self.coordinates objectAtIndex:[self.coordinates indexOfObject:[self.coordinates lastObject]]];
last.tag=99;

然后如您所见,我对viewForAnnotation进行了一些修改,以了解注释是我所寻找的. 然后,窍门就是调用在后台线程中添加批注的函数,例如:

Then as you can see i have slightly modified the viewForAnnotation to understand the annotation was the one I looked for. Then the trick was just calling the function that adds the annotations in a background thread, like:

[self performSelectorInBackground:@selector(addAllAnnot) withObject:nil];

-(void)addAllAnnot{

    [self.myMap addAnnotations:self.coordinates];
}

经过一个星期的测试,这对我有用,如果有人有更好的主意,将不胜感激.

This worked for me after a week of testing, if anyone has any better idea, it will be appreciated.

推荐答案

看看您在答案中提到的答案,我认为您无法确定添加这些注释的顺序或它们的索引在mapview的内部列表中.

Looking at the answer you referred in your answer I think there is no way by which you can determine the order in which these annotations are added or what index do they have in the internal list of the mapview.

那么您有什么选择呢?一如既往的非常基础-对MKAnnotation类进行子类化,并向其中添加一个属性,通过该属性可以区分要添加的注释.因此,只需在将这些批注添加到数组中时设置属性,希望这可以解决您的问题.

So what option do you have..? As always the very fundamental-- Sub-classing the MKAnnotation class and adding a property to it through which you can differentiate which annotation is being added. So just set the property when you are adding these annotations in the array... hoping this solves your problem..

这篇关于仅针对MKMapView上的第一个和最后一个MKAnnotation设置MKPinAnnotationColorRed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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