MKMapView屏幕外注释图像不正确 [英] MKMapView Off Screen Annotation Image Incorrect

查看:125
本文介绍了MKMapView屏幕外注释图像不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张地图,我添加了几个注释,如下所示:

I have a map to which I add several annotations, like so:

    for (Users *user in mapUsers){

        double userlat = [user.llat doubleValue];
        double userLong = [user.llong doubleValue];

        CLLocationCoordinate2D userCoord = {.latitude =  userlat, .longitude =  userLong};

        MapAnnotationViewController *addAnnotation = [[MapAnnotationViewController alloc] initWithCoordinate:userCoord];

        NSString *userName = user.username;
        NSString *relationship = user.relationship;

        [addAnnotation setTitle:userName];
        [addAnnotation setRelationshipParam:relationship];

        [self.mainMapView addAnnotation:addAnnotation];
    }

使用此委托方法代码:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *identifier = @"AnnotationIdentifier";

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

    MKAnnotationView *annView = (MKAnnotationView *)[self.mainMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annView) {
        annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:identifier];
    }
    MapAnnotationViewController *sac = (MapAnnotationViewController *)annView.annotation;

    if ([sac.relationshipParam isEqualToString:@"paramA"])
    {
        annView.image = [UIImage imageNamed:@"image1.png"];
    }
    else if ([sac.relationshipParam isEqualToString:@"paramB"])
    {
        annView.image = [UIImage imageNamed:@"image2.png"];
    }
    else if ([sac.relationshipParam isEqualToString:@"paramC"])
    {
        annView.image = [UIImage imageNamed:@"image3.png"];
    }


    return annView;
}
else {
    return nil;
}

这一切都适用于地图的原始加载。但是,当我选择注释(其自定义代码太长而无法发布但包括放大)时,先前绘制的注释图像已更改图标。不会重绘地图,并且不会在该过程中重新添加注释。当我在地图上捏回来时,图像是不同的(它们与不正确的关系params匹配错误的image1-3.png的。

This all works fine on the original loading of the map. However, when I select the annotation (which has custom code that is too long to post but includes a zoom in) the annotation images that were previously drawn have changed icons. The map is not redrawn and the annotations are not re-added in that process. When I pinch back out on the map, the images are different (they have match the incorrect relationship params with the wrong image1-3.png's.

任何人都可以想到为什么这是发生了什么,或者要寻找什么?

Can anyone think of why this is happening, or what to look for?

推荐答案

dequeueReusableAnnotationViewWithIdentifier 可能会返回一个注释视图,该注释视图用于与当前注释参数不同的注释。

The dequeueReusableAnnotationViewWithIdentifier may return an annotation view that was used for an annotation different from the current annotation parameter.

如果 dequeueReusableAnnotationViewWithIdentifier 是成功的(即您正在使用以前使用的注释视图),您必须将其注释属性更新为确保视图与当前注释的属性匹配。

If the dequeueReusableAnnotationViewWithIdentifier is succesful (ie. you're using a previously-used annotation view), you must update its annotation property to be sure the view matches the current annotation's properties.

因此请尝试更改此部分:

So try changing this part:

MKAnnotationView *annView = (MKAnnotationView *)[self.mainMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annView) {
    annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:identifier];
}
MapAnnotationViewController *sac = (MapAnnotationViewController *)annView.annotation;

to:

MKAnnotationView *annView = (MKAnnotationView *)[self.mainMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annView) {
    annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:identifier];
}
else {
    annView.annotation = annotation; // <-- add this
}

MapAnnotationViewController *sac = (MapAnnotationViewController *)annView.annotation;



另一个潜在问题(不会导致问题出现问题)是仅当 relationshipParam 是三个值之一时,才会设置视图的图像属性。


Another potential issue (not causing the problem in the question) is that the view's image property is only set if relationshipParam is one of three values.

如果以某种方式 relationshipParam 不是这三个编码值中的一个,那么视图就是出列,图像将基于其他一些注释的 relationshipParam

If somehow relationshipParam is not one of those three coded values and the view was dequeued, the image will be based on some other annotation's relationshipParam.

所以你应该添加一个 else 部分设置 image 并将其设置为某个默认图像以防万一:

So you should add an else part to the section that sets image and set it to some default image just in case:

...
else if ([sac.relationshipParam isEqualToString:@"paramC"])
{
    annView.image = [UIImage imageNamed:@"image3.png"];
}
else
{
    annView.image = [UIImage imageNamed:@"UnknownRelationship.png"];
}

这篇关于MKMapView屏幕外注释图像不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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