0

我正在尝试使用多个不同的自定义地图图钉渲染地图。

按照此处提供的文档,我已设法将所有图标替换为自定义图标:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/map-pin

但是我希望实现的是根据我的 CustomPin 类的属性值更改图标,问题似乎是在渲染我的地图时,标记被创建但我的 CustomPin 列表仍然是“null”,我试过了查找有关基于自定义属性的多个图标的更多文档或答案,但未成功。

谁能给我推动正确的方向?

代码片段,目前无法按上述方式工作

自定义引脚

    public class CustomPin : Pin
    {
        public string IconType { get; set; }
    }

自定义地图

     public class CustomMap : Map
    {
        public List<CustomPin> CustomPins { get; set; }
    }

MapPage.xaml.cs(填充 pin 列表 => IconType 是我的自定义属性,用于检查要呈现的图标)

    private void DisplayOnMap(IList<Models.Location> pins)
        {
            List<CustomPin> CustomPins = new List<CustomPin>();
            foreach (var item in pins)
            {
                try
                {
                    var position = new Xamarin.Forms.Maps.Position(item.lat, item.lon);
                    var pin = new CustomPin()
                    {
                        Type = Xamarin.Forms.Maps.PinType.Generic,
                        Position = position,
                        Label = item.naam,
                        Address = $"{item.straat} {item.nummer}, {item.postnr} {item.stad} ({item.land})",
                        IconType = item.soort
                    };
                    CustomPins.Add(pin);
                }
                catch (NullReferenceException nre)
                {
                    Console.WriteLine(nre.Message);
                }
                catch (Exception ex)
                {

                }
            }
            locationsMap.CustomPins = CustomPins;
            foreach (var cpin in locationsMap.CustomPins)
            {
                locationsMap.Pins.Add(cpin);
            }
        }

自定义渲染器 (Android)

    public class CustomMapRenderer : MapRenderer
    {
        List<CustomPin> customPins;
        public CustomMapRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
            }

            if (e.NewElement != null)
            {
                
            }
        }
        protected override MarkerOptions CreateMarker(Pin pin)
        {
            var marker = new MarkerOptions();
            marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
            marker.SetTitle(pin.Label);
            marker.SetSnippet(pin.Address);
            var customPin = GetCustomPin(pin);
            if (customPin != null)
            {
                if(customPin.IconType == "corporate")
                {
                    marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.corporate));
                }
                else if(customPin.IconType == "pickup")
                {
                    marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.bus));
                } 
            }
           
            return marker;
        }


        CustomPin GetCustomPin(Pin pin)
        {
            var position = pin.Position;
            foreach (var cpin in customPins)
            {
                if (cpin.Position == position)
                {
                    return cpin;
                }
            }
            return null;
        }
    }
}
4

1 回答 1

1

此问题的解决方案是直接选择地图元素的 Custompins 属性以将正确的 CustomPin 与默认 Pins 配对。

protected override MarkerOptions CreateMarker(Pin pin)
    {
        var marker = new MarkerOptions();
        marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
        marker.SetTitle(pin.Label);
        marker.SetSnippet(pin.Address);
        var customPin = (Element as CustomMap).CustomPins.FirstOrDefault(p => p.Position == pin.Position);
        if (customPin != null)
        {
            if (customPin.IconType == "corporate")
            {
                marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.corporate));
            }
            else if (customPin.IconType == "pickup")
            {
                marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.bus));
            }
        }

        return marker;
    }
于 2021-03-06T18:09:17.827 回答