MKAnnotationView错误的图像显示

问题描述 投票:0回答:1

我有四种类型的自定义注释。我是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 但是在我的情况下无法弄清楚。

xamarin.ios mkmapview mkannotation mkannotationview
1个回答
0
投票

在重用annotationView时你做得不对,我们应该使用不同的resuseId来分类custom viewdefault view

Code

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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.