地图视图标注不同颜色的脚 [英] Map view annotations with different pin colors

查看:184
本文介绍了地图视图标注不同颜色的脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有超过200个对象的数组,我试图通过他们每个人来执行循环。

I have an array with over 200 objects and I am trying to perform a loop through each of them.

每个对象有一个是/否字段和我想要显示依赖于该是/否值不同颜色的标记。

Each object will have a yes/no field and I want to display a different coloured marker dependent on that yes / no value.

这是我所看到的正在发生的事情我的循环正在经历每个第一,然后对象的所有标注在年底对每个对象添加。

From what I can see is happening my loop is going through each object first and then all the annotation is added at the end for each object .

由于我通过阵列上的是当所有的注释被添加到我的地图没有价值,它会使用是/否从阵列中的最后一个对象值执行我的循环内进行检查时,去积所有。

Since I perform a check within my loop through the array on the yes no value when all the annotation is added to my map, it will use the yes/no value from the last object in the array when it goes to plot for all.

如何可以拥有它,这样标记会有所不同取决于是/为每个单独的元素没有价值?

How can I have it so that the marker will be different dependent on the yes/no value for each individual element?

我的code是

for (i = 0; i < [appDelegate.itemArray count]; i++) {
        item_details *tempObj = [appDelegate.itemArray objectAtIndex:i];
        location.latitude = [tempObj.lat floatValue];
        location.longitude = [tempObj.lon floatValue];
        current_yesno = tempObj.yesno;
        MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc]initWithTitle:tempObj.name andCoordinate:location];
        [self.mapView addAnnotation:newAnnotation];
        [newAnnotation release];            
            } 

我的注释code如下:

with my annotation code as follows

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

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

if(current_yesno == YES){
    annView.pinColor = MKPinAnnotationColorGreen;
}
else
{
    annView.pinColor = MKPinAnnotationColorRed;
}
    annView.animatesDrop=NO;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;

}

current_yesno 在我.h文件中声明。

and current_yesno is declared in my .h file.

推荐答案

viewForAnnotation 委托方法不一定叫后立即您 addAnnotation ,它也可以通过时,它需要获得视图注释(而你的code是做一些完全不同的)地图视图在其他时间调用。

The viewForAnnotation delegate method isn't necessarily called immediately after you do addAnnotation and it can also be called at other times by the map view when it needs to get the view for an annotation (while your code is doing something completely different).

所以你不能依赖于同步伊娃正在与委托方法之外的一些code的值。

So you can't depend on the value of an ivar being in sync with some code outside that delegate method.

相反, YESNO 属性添加到您的自定义 MapViewAnnotation 类,创建注释,然后访问时,将其设置通过注释参数及其 viewForAnnotation 值(即地图视图是给你确切的注释对象的引用它想要的视图)。

Instead, add the yesno property to your custom MapViewAnnotation class, set it when creating the annotation and then access its value in viewForAnnotation through the annotation parameter (ie. the map view is giving you a reference to the exact annotation object it wants the view for).

例如:

MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] init...
newAnnotation.yesno = tempObj.yesno;  // <-- set property in annotation
[self.mapView addAnnotation:newAnnotation];

然后在 viewForAnnotation

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    if (![annotation isKindOfClass:[MapViewAnnotation class]])
    {
        // Return nil (default view) if annotation is 
        // anything but your custom class.
        return nil;
    }

    static NSString *reuseId = @"currentloc";

    MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (annView == nil)
    {
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];        
        annView.animatesDrop = NO;
        annView.canShowCallout = YES;
        annView.calloutOffset = CGPointMake(-5, 5);
    }
    else
    {
        annView.annotation = annotation;
    }

    MapViewAnnotation *mvAnn = (MapViewAnnotation *)annotation;
    if (mvAnn.yesno)
    {
        annView.pinColor = MKPinAnnotationColorGreen;
    }
    else
    {
        annView.pinColor = MKPinAnnotationColorRed;
    }

    return annView;
}

这篇关于地图视图标注不同颜色的脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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