【问题标题】:Map Annotations ars displaying with incorrect custom pin images地图注释显示不正确的自定义图钉图像
【发布时间】:2013-02-27 18:54:48
【问题描述】:

这是我在这个网站上的第一个问题。

我的代码是使用 iOS 6 Mapkit,Objective-C 在以下函数中实现的。

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

我只有两个带有自定义图钉图像的注释。一个红色别针和另一个橙色别针。红色大头针更接近用户在设备地图区域内的当前位置,而另一个橙色大头针距离 30 英里(当前地图上下文之外)。我根据每个引脚后面的数据使用不同的颜色。

问题:注释的自定义图像正在切换图像。

使用本网站上的所有提示,包括使用 dequeueReusableAnnotationViewWithIdentifier 等附加是我的代码。

在应用程序启动时地图的第一次显示上,我看到的错误是橙色图钉显示在设备地图上,而红色图钉显示在外面。这是不正确的,因为红色图像应该显示在设备地图上。

如果我点击地图上的“查找我”按钮来刷新我的当前位置,地图上会显示红色图钉,现在是正确的。当我再次点击“查找我”时,红色引脚会变为橙色引脚 - 并且它会不断切换或切换引脚图像颜色。

if (PinColor == 0) {
            MKAnnotationView* pinview = (MKAnnotationView *)[self._mapView dequeueReusableAnnotationViewWithIdentifier:@"identifier"];

            if (nil == pinview) {                    

                MKAnnotationView* view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"identifier"];

            view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

            view.opaque = YES;
            view.image = [UIImage imageNamed:@"redpin.png"];

            [view setCanShowCallout:YES];                

            if (self.newPinAdded) {
                [view setSelected: YES];                    
            }

            return view;
        }

        return pinview;            
    }
    else {

        MKAnnotationView* pinview = (MKAnnotationView *)[self._mapView dequeueReusableAnnotationViewWithIdentifier:@"Orangeidentifier"];

        if (nil == pinview) {                

            MKAnnotationView* view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Orangeidentifier"];

            view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

            view.image = [UIImage imageNamed:@"pin glabensmall.png"]; //orange pin

            view.opaque = YES;
            [view setCanShowCallout:YES];


            if (self.newPinAdded) {
                [view setSelected: YES];                    
            }

            return view;
        }

        return pinview;
    }

【问题讨论】:

    标签: objective-c annotations mkmapview mapkit


    【解决方案1】:

    我认为您的问题很常见。 PinColor 在哪里设置? mapview 地图要求 annotationView 随时为任何注解绘制注解。如果通过其他方法将 PinColor 设置为 0,然后地图视图想要绘制注释 任何注释,它将绘制一个红色图钉。您需要做的是检查您正在绘制的注释,然后使用正确的颜色。您可以通过阅读其标题来检查注解,或者如果它是您自己的注解类,则可以使用其他一些属性。

    旁注:您在两个引脚版本中都重复了几行,您应该将它们放在 IF 语句之外并删去一些行。你应该使用viewForAnnotation给你的mapView:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation{
    
    MKAnnotationView* pinview = nil;
    if (annotation is the red one) {
        pinview = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"identifier"];
        if (nil == pinview) {                 
            pinview = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"identifier"];
            pinview.image = [UIImage imageNamed:@"redpin.png"];
         }        
    } else {
       pinview = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Orangeidentifier"];
        if (nil == pinview) {                
            pinview = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Orangeidentifier"];
            pinview.image = [UIImage imageNamed:@"pin glabensmall.png"]; //orange pin           
        }
    }
    
    [pinview setCanShowCallout:YES];                
    
    pinview.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    
    pinview.opaque = YES;
    if (self.newPinAdded) {
        [pinview setSelected: YES];                    
    }
    
    }
    

    【讨论】:

    • 我没有设置任何 PinColor。甚至出现的红色图钉也是我自定义的红色图钉,而不是 mapkit 中默认出现的标准红色图钉。
    • 您的代码以if(PinColor == 0) 开头。何时 - 您生成红色图像,否则生成橙色图像。问题是您没有更改 PinColor 以匹配您正在绘制的注释。
    • 完美答案。您提到我应该在设置相应图像之前检查注释属性(即 annotation.title 或 annotation.subtitle)的回答非常有帮助。我更新了我的 annotation.title 以容纳可以帮助我做到这一点的文本,现在它工作正常。感谢您的快速帮助。
    • 很高兴为您服务。现在你需要点击答案旁边的勾选/复选标记,这样每个人都知道它已经解决了(所以我得到了积分:))
    猜你喜欢
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多