MKMapView在引脚颜色注释之间切换

时间:2014-05-07 17:23:44

标签: ios ios7 mkmapview mkannotation

我的地图上有一堆别针,有些是红色,有些是绿色的。初始着色没问题。但是,当我使用地图视图并点击一些绿色视图时,一些红色将变为绿色。当它们位于当前视图区域之外并移动到视图区域时,可能会发生这种情况。 任何想法?

我的代码片段来自:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id < MKAnnotation >)annotation{

    if(((MyAnnotation*)annotation).isGreen){
        AnnotationViewID = @"MyAnnotationGreen";
    }else{
        AnnotationViewID = @"MyAnnotationRed";
    }

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
    }

1 个答案:

答案 0 :(得分:0)

您应该使用MyAnnotation对象的isGreen属性为MKPinAnnotationView对象指定颜色。重用代码可以为您提供一个没有正确设置所有属性的对象,尤其是因为您在不同的标识符下使用完全相同的类。您不应该依赖视图缓存中的出列对象的所有属性值的有效性(例如uitableview dequeue)。

做类似的事情:

MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

if (annotationView == nil)
{
    annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}
[annotationView setPinColor:(annotation.isGreen)? MKPinAnnotationColorGreen : MKPinAnnotationColorRed];