MKAnnotationView错误的图像显示

时间:2018-01-02 00:18:11

标签: xamarin.ios mkmapview mkannotation mkannotationview

我有四种类型的自定义注释。我是PointAnnotation的子类,它将定义注释的枚举类型。自定义类型是我从后端获得的注释,并动态添加它们。添加注释很好。但是当我移动地图时,注释的图像会发生变化,我也注意到它改变了注释的类型。

首先,当我从后端获得所有注释时,它看起来像这样> enter image description here

然后我添加一个自定义注释 enter image description here

当我缩小地图注释时,最近的注释图像会更改为最近的注释。 enter image description here

我的代码是:

override public MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
    {
         string resuseId = "";

         MKAnnotationView annotationView = mapView.DequeueReusableAnnotation(resuseId);

        if (ThisIsTheCurrentLocation(mapView, annotation))
        {
            return null;
        }

        if (annotationView == null)
        {

            if (annotation is CustomAnnotation)
            {
                switch (CustomAnnotation.MarkerType)
                {
                    case MyMarkerType.Note:
                        annotationView = new MKAnnotationView(annotation, resuseId = "Note");
                        annotationView.Image = UIImage.FromBundle("Message");
                        annotationView.UserInteractionEnabled = true;
                       // CGPointMake(0, -imageHeight / 2).
                        annotationView.CanShowCallout = false;
                        break;
                    case MyMarkerType.Photo:
                        annotationView = new MKAnnotationView(annotation, resuseId = "Photo");
                        annotationView.Image = UIImage.FromBundle("Photo");
                        annotationView.CanShowCallout = false;
                        break;
                    case MyMarkerType.Story:
                        annotationView = new MKAnnotationView(annotation, resuseId = "Story");
                        annotationView.Image = UIImage.FromBundle("Story");
                        annotationView.CanShowCallout = false;
                        break;
                    case MyMarkerType.Custom:
                        annotationView = new MKAnnotationView(annotation, resuseId = "Custom");
                        NSData data = NSData.FromArray(CustomAnnotation.WayPoint.Image);
                        UIImage image = UIImage.LoadFromData(data);
                        // UIImage finalImage = image.MaxResizeImage(21f, 20f);
                        annotationView.Image = image;
                        annotationView.CanShowCallout = false;
                        break;

                    default:
                        annotationView.Annotation = annotation;
                        break;
                }
            }

            else  //not custom view
            {
                annotationView = new MKMarkerAnnotationView(annotation, "cluster");
            }

        }

        else
        {
            annotationView.Annotation = annotation;

        }

         annotationView.CanShowCallout = false;
        return annotationView;

    }

我看了一下这个答案 MKAnnotationView image changing issue 但在我的案例中无法理解。

1 个答案:

答案 0 :(得分:0)

重复使用annotationView时你做错了,我们应该使用不同的resuseId来分类custom viewdefault view

代码

override public MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
    {
        MKAnnotationView annotationView = null;

        if (ThisIsTheCurrentLocation(mapView, annotation))
        {
            return null;
        }

        if (annotation is CustomAnnotation) //custom view
        {
            annotationView = mapView.DequeueReusableAnnotation("CustomID");

            if (annotationView == null)
            {
                annotationView = new MKAnnotationView(annotation, resuseId = "CustomID");
            }

            switch (CustomAnnotation.MarkerType)
            {
                case MyMarkerType.Note:

                    annotationView.Image = UIImage.FromBundle("Message");
                    annotationView.UserInteractionEnabled = true;
                    break;
                case MyMarkerType.Photo:
                    annotationView.Image = UIImage.FromBundle("Photo");
                    break;
                case MyMarkerType.Story:
                    annotationView.Image = UIImage.FromBundle("Story");
                    break;
                case MyMarkerType.Custom:
                    NSData data = NSData.FromArray(CustomAnnotation.WayPoint.Image);
                    UIImage image = UIImage.LoadFromData(data);
                    // UIImage finalImage = image.MaxResizeImage(21f, 20f);
                    annotationView.Image = image;
                    break;
                default:
                    annotationView.Annotation = annotation;
                    break;
            }

            annotationView.Annotation = annotation;
        }

        else  //not custom view
        {
            annotationView = mapView.DequeueReusableAnnotation("DefaultID");

            if(annotationView == null)
            {
                annotationView = new MKMarkerAnnotationView(annotation, "DefaultID");
            }

            annotationView.Annotation = annotation;   
        }

        annotationView.CanShowCallout = false;
        return annotationView;
    }