简体   繁体   English

如何在 Xamarin Forms 地图上添加多个不同的自定义图标?

[英]How to add multiple different custom icons on Xamarin Forms Maps?

I'm trying to render a map with multiple different custom map pins.我正在尝试使用多个不同的自定义 map 引脚来渲染 map。

I have managed to replace all my icons to a custom icon following the documentation provided here:按照此处提供的文档,我已设法将所有图标替换为自定义图标:

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

However what I wish to achieve is to change the icon based on a property value off my CustomPin class , the problem seems to be that when rendering my map, the markers get created but my CustomPin List is still "null", I've tried to find more documentation or answers about multiple icons based on a custom property without success.但是我希望实现的是根据我的 CustomPin class 的属性值更改图标,问题似乎是在渲染我的 map 时,创建了标记,但我的 CustomPin 列表仍然是“null”,我试过了查找有关基于自定义属性的多个图标的更多文档或答案,但未成功。

Could anyone give me a push in the right direction?谁能给我推动正确的方向?

Code snippets, which are currently not working as stated above :代码片段,目前无法按上述方式工作

CustomPin自定义引脚

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

CustomMap自定义地图

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

MapPage.xaml.cs (populating the pin lists => IconType is my custom property to check which icon to render) MapPage.xaml.cs(填充引脚列表 => 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);
            }
        }

CustomRenderer (Android)自定义渲染器 (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;
        }
    }
}

The solution to this problem was to directly select the map-element's Custompins property to pair the correct CustomPin to the default Pins.此问题的解决方案是直接 select 映射元素的 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM