iphone - 仅为 MKMapView 上的第一个和最后一个 MKAnnotation 设置 MKPinAnnotationColorRed

标签 iphone ios mkmapview mkannotationview mkpinannotationview

我有一个自定义MKAnnotationNSArray,我需要为第一个/最后一个注释设置红色引脚颜色,否则设置绿色引脚。 该程序没有按照我想要的方式运行,绿色引脚每次都以随机方式与两个不同的注释相关联,而不是按照我想要的方式与第一个和最后一个注释相关联。

所以,这就是我在 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:回调中:

- (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: 中进行类似的过程,但没有好的结果。

一些提示? 谢谢。

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

编辑:经过几次测试,我意识到实现我想要的最好方法是首先为我的第一个/最后一个注释设置一个标签:

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

然后,如您所见,我稍微修改了 viewForAnnotation 以了解该注释就是我要查找的注释。 然后,技巧就是调用在后台线程中添加注释的函数,例如:

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

-(void)addAllAnnot{

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

经过一周的测试,这对我有用,如果有人有更好的想法,我们将不胜感激。

最佳答案

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

那么你有什么选择..?与往常一样,最基础的——对 MKAnnotation 类进行子类化并向其添加一个属性,通过该属性您可以区分正在添加哪个注释。因此,只需在数组中添加这些注释时设置属性...希望这可以解决您的问题..

关于iphone - 仅为 MKMapView 上的第一个和最后一个 MKAnnotation 设置 MKPinAnnotationColorRed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10056677/

相关文章:

iphone - iOS API在4.x和5.x之间的区别

Iphone GPS 没有提供准确的纬度和经度

iphone - MKOverlay View 模糊

iphone - MKMapView 关闭位置服务

ios - 如何有选择地选择与Xcode 7.5 beta兼容的设备

iphone - 关于以编程方式创建 UITextField 的问题

iphone - IOS 6 中的引用错误

ios - MapKit - 更新 MapView 以通过计时器显示移动注释

objective-c - 如何将 NIB 文件中的对象发送到前/后?

xcode - 如何仅在链接时有条件地使用静态库