缩放后MKMapview注释动态图像变化 [英] MKMapview annotation dynamic pin image changes after zooming

查看:119
本文介绍了缩放后MKMapview注释动态图像变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小项目,在地图上显示7种不同类型的注释。我的注释取自数组中的url结果,我使用JSON解析它。我有很多注释,一旦地图加载,一切看起来都很好。放大和缩小后,引脚图像由于某种原因改变为错误的引脚图像(特定图像,没有线索原因)。

I am working on a little project that shows 7 different types of annotations on the map. My annotations are taken from a url result in array and I parse it using JSON. I have lots of annotations and everything seems to look good once the map loads. After zooming in and zooming out, the the pin images changes for some reason to the wrong pin image (a specific image, no clue why).

我相信我是在这里遗漏了一些东西......请你帮忙:)?

I am sure I am missing something here...may you please help :) ?

这是我的代码的一部分,如果您需要它,请告诉我:

Here's one part of my code, let me know if you need anymore of it:

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

static NSString *identifier;

if(_mapView.tag==1){identifier = @"TurbulencePin";}
if(_mapView.tag==2){identifier = @"IcingPin";}
if(_mapView.tag==3){identifier = @"WindsPin";}
if(_mapView.tag==4){identifier = @"TemperaturePin";}
if(_mapView.tag==5){identifier = @"CloudsPin";}
if(_mapView.tag==6){identifier = @"VisibilityPin";}
if(_mapView.tag==7){identifier = @"MultiplePin";}


if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

if ([annotation isKindOfClass:[Annotation class]]) {

    CustomAnnotationView* annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    annotationView = nil;

    if (annotationView == nil) {

        annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;


        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",identifier]];
        annotationView.image = img;

    }
else
    {

        annotationView.annotation = annotation;

    }


    return annotationView;

}
return nil;

}






更新:

根据其他人的反馈,我修改了图片设置的代码如下:

Based upon feedback of others, I've modified the code for the image setting to be as follows:

 Annotation *myCustomAnn = (Annotation *)annotation;
 NSString *imgName = myCustomAnn.imageName;
 UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@Pin.png",imgName]];
 annotationView.image = img;

 return annotationView;

另外,我删除了 annotationView = nil;

但是,我无法将annotation.m中的图像名称设置为硬编码值,因为我需要为每个注释显示不同的图像。我确定有一个解释,但我可以从mapView:viewForAnnotation下的annotation.m获得的唯一值是注释坐标( myCustomAnn.coordinate.latitude myCustomAnn.coordinate.longitude),我不知道如何从annotation.m获取其他属性

However, I cannot set the image name in the annotation.m as a hardcoded value because I need to display a different pin image for each annotation. I'm sure that there is an explanation but the only value that I can get from the annotation.m under the mapView:viewForAnnotation: is the annotation coordinates (myCustomAnn.coordinate.latitude and myCustomAnn.coordinate.longitude), I have no clue how to get other properties from the annotation.m

其他属性,例如title,imgname等返回为null

The other properties, such as title, imgname etc comes back as null

推荐答案

一个问题是 viewForAnnotation 正在确定基于类实例变量显示的正确图像。通常,注释图像的标识符将是自定义注释本身的属性,而不是某些外部实例变量。

One issue is that the viewForAnnotation is determining the correct image to show based upon a class instance variable. Generally the identifier for the annotation's image would be a property of the custom annotation itself, not some external instance variable.

除此之外,似乎注释正在存在在设置所有注释的属性之前添加到地图。应该推迟 addAnnotation ,直到设置了所有注释的属性。

On top of that, it appeared that the annotation was being added to the map before all of the annotation's properties were being set. One should defer the addAnnotation until all of the annotation's properties are set.

或者,您可以将注释添加到 NSMutableArray ,根据需要调整它们,并且只添加最后使用 addAnnotations 注释(注意s),将数组传递给它。

Alternatively, you can add the annotations to a NSMutableArray, tweak them as you see fit, and only add the annotations at the very end using the addAnnotations (note the s), passing it the array.

这篇关于缩放后MKMapview注释动态图像变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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