放大/缩小时MKAnnotationView错误更改了图钉图像 [英] MKAnnotationView fault when zoom in/out changed the pin image

查看:85
本文介绍了放大/缩小时MKAnnotationView错误更改了图钉图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了注释贴图并为引脚使用了多个图像,但每当我放大或缩小时,它都会将所有引脚更改为一个图像。

I have used annotation map and used more than one image for the pins but whenever I zoom in or zoom out, it changes all the pins to one image.

我从Web服务获取位置并识别它们,我使用字符串( CustAttr )作为T或P。

I get the locations from a web service and to recognise them, I used a string (CustAttr) as "T" or "P".

问题是来自Web服务的最后一次调用使 CustAttr = T ,当我放大或缩小时,它调用mapView viewForAnnotation 方法并将它们全部绘制为 T 以及所有 P 引脚已更改。

The problem is the last call from a web service makes the CustAttr = T and when I zoom in or zoom out, it calls the mapView viewForAnnotation method and draws them all as T and all the P pins are changed.

以下是该方法的代码:

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

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

}
static NSString* AnnotationIndentifer = @"AnnotationIdentifier";



if ([custAttr isEqualToString:@"T"]) // ATMs
{
    MKAnnotationView* pinView;
    pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIndentifer];

    MapAnnotation* mapAnnotation = annotation;
    pinView.canShowCallout = YES;

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    pinView.rightCalloutAccessoryView = rightButton;

    if (mapAnnotation.isClosest) {
        pinView.image = [UIImage imageNamed:@"Closest_ATM.png"];

    }
    if (mapAnnotation.isOffline) {
        pinView.image = [UIImage imageNamed:@"Offline_ATM.png"];
    }
    pinView.annotation = annotation;
    return pinView;        

}else if ([custAttr isEqualToString:@"P"]) // POIs
{
    MKAnnotationView* pinView;
    pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIndentifer];

    pinView.canShowCallout = YES;
    pinView.image = [UIImage imageNamed:@"Location_POI.png"];
    pinView.annotation = annotation;
    return pinView;
}

return nil;
}

如何解决此问题?是否有一种方法可以阻止它在放大/缩小时调用此方法,或者是否有另一种方法让它在同一图像中再次绘制它们?

How can I resolve this issue? Is there a way that I can prevent it from calling this method when zooming in/out or is there another way to let it draw them again as in the same image?

推荐答案

custAttr 变量(您在委托方法之外设置)并不总是与同步注释调用 viewForAnnotation 委托方法。

The custAttr variable (which you are setting outside the delegate method) will not always be in sync with the annotation that the viewForAnnotation delegate method is called for.

委托方法不是必须在 addAnnotation addAnnotations 之后立即调用,并且如果地图需要显示注释,则可以为每个注释多次调用在缩放或平移后再次查看。

The delegate method is not necessarily called right after addAnnotation or addAnnotations and can be called multiple times for each annotation if the map needs to display the annotation view again after a zoom or pan.

当再次调用相同的注释时, custAttr 变量no更长时间匹配。

When it gets called again for the same annotation, the custAttr variable no longer matches up.



您需要添加 custAttr 属性(我建议使用您的 MapAnnotation 类的不同名称,并在创建annota时设置它(在调用 addAnnotation 之前)。


You need to add a custAttr property (I suggest using a different name) to your MapAnnotation class and set it when creating the annotation (before calling addAnnotation).

例如:

MapAnnotation *ann = [[MapAnnotation alloc] init];
ann.coordinate = ...
ann.title = ...
ann.subtitle = ...
ann.custAttr = custAttr; // <-- copy to the annotation object itself
[mapView addAnnotation:ann];



然后,在 viewForAnnotation ,从 注释 参数中读取 custAttr 属性(在将其转换为 MapAnnotation * )而不是引用外部声明的 custAttr


Then, in viewForAnnotation, read the custAttr property from the annotation parameter (after casting it to MapAnnotation *) instead of referencing the externally declared custAttr.

您可能希望在 MapAnnotation 中为 custAttr 属性使用不同的名称,以避免混淆。

You may want to use a different name for the custAttr property in MapAnnotation to avoid confusion.

这篇关于放大/缩小时MKAnnotationView错误更改了图钉图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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