【问题标题】:Xamarin iOS - MKPinAnnotationView.Image not displaying custom ImageXamarin iOS - MKPinAnnotationView.Image 不显示自定义图像
【发布时间】:2017-09-29 17:53:14
【问题描述】:

我正在编写一个自定义地图渲染器,它扩展了 Xamarin 的 Map 类,目前正在尝试将自定义图像添加到我的应用程序中 iOS 地图上显示的一些图钉中。

我的 iOS 解决方案中的 resources 文件夹中有一个名为 Images 的文件夹,然后我使用 Add Existing 添加了我的 fire.png 文件。在我的代码中,当我在图像设置为 pinAnnotationView 的位置设置断点时,您可以看到图像不为空并且不应恢复为通用 iOS 引脚,但是我的火图像未显示且默认引脚是。

这是我的代码:

public MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
        {
            var myAnnotation = annotation as BasicMapAnnotation;
            var annotationView = new MKPinAnnotationView(myAnnotation, "basic annotation");

            var pinAnnotationView = annotationView as MKPinAnnotationView; 

            if (pinAnnotationView != null)
            {
                if (myAnnotation?.IsEmergency == "1")
                {
                    pinAnnotationView.PinTintColor = UIColor.Blue;

                }
                if (myAnnotation?.IsFireHazard == "1")
                {
                    //show fire image
                    UIImage image = UIImage.FromFile("Images/fire.png");
                    pinAnnotationView.Image = image;
                }
            }
            return pinAnnotationView;
        }

当我调试时:

【问题讨论】:

    标签: c# xamarin xamarin.ios xamarin.forms


    【解决方案1】:

    不要使用MKPinAnnotationView设置图片,参考here

    我修改你的代码如下

    public MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
    {
        string reuseIdentifier = "basic annotation";
    
        var myAnnotation = annotation as BasicMapAnnotation;
    
        var annotationView = new MKAnnotationView(myAnnotation, reuseIdentifier);
    
        if (annotationView != null)
        {
            if (myAnnotation?.IsEmergency == "1")
            {
                MKPinAnnotationView annotationPinView = annotationView as MKPinAnnotationView;
                annotationPinView.PinTintColor = UIColor.Blue;
                return annotationPinView;
            }
            if (myAnnotation?.IsFireHazard == "1")
            {
                //show fire image
                UIImage image = UIImage.FromFile("Images/fire.png");
                annotationView.Image = image;
                return annotationView;
            }
        }
        return null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多