Issue with the MapElements for UWP Maps control

309 views Asked by At

The funny thing is stack gives me a warning saying "questions with a matching tittle have been closed for being too broad", So I'll try to keep it on point.

What is the issue:

  • On my map control, I am adding a polyline (maybe 3, depending upon response from server).
  • Now when I add MapIcons or any MapElement or a MapItemsControl (basically anything) it doesn't show up. After debugging, I found out even though the MyMap.MapElements.Add(myMapIcon); runs perfectly, the MyMap.MapElements.Count only shows the polyline(s). Only the polyline is added.
  • So I removed my Polyline and guess what all the above mentioned components they all are added to the MapControl and are visible.
  • I dug a little deeper and found out that each time I call my MyMap.MapElements.Add(myMapIcon) it calls to add the already added polyline as polylines are added via attached properties and binding.

My Code:

My Polyline creator attached class:

 public class PolylineCollection
{

    public static readonly DependencyProperty PathDataCollectionProperty =
        DependencyProperty.Register("PathDataCollection", typeof(ObservableCollection<IPolylinePath>), typeof(PolylineCollection), new PropertyMetadata(null, OnPathCollectionChanged));


    public static void SetPathCollection(UIElement element, ObservableCollection<IPolylinePath> value)
    {
        element.SetValue(PathDataCollectionProperty, value);
    }


    public static ObservableCollection<IPolylinePath> GetPathCollection(UIElement element)
    {
        return (ObservableCollection<IPolylinePath>)element.GetValue(PathDataCollectionProperty);
    }




    private static void OnPathCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e?.NewValue != null)
        {
            if (e.NewValue is IEnumerable<IPolylinePath> polylineCollection)
            {

                var mapControl = d as MapControl;

                if (mapControl == null)
                {
                    throw new InvalidOperationException(
                        "Polyline.Track property can only be attached to a MapControl!");
                }

                mapControl.MapElements.Clear();

                foreach (var polyline in GetPathCollection(mapControl))
                {
                    mapControl.MapElements.Add(CreateMapPolyline(polyline));
                }
            }
        }
    }



    private static MapPolyline CreateMapPolyline(IPolylinePath track)
    {
        var polyline = new MapPolyline()
        {
            StrokeColor = track.PolylineColor.Color,
            StrokeThickness = track.PolylineThinkness,
            StrokeDashed = false,
            Path = track.PolylineGeopath,
        };

        if (track.PolylineColorMode == Enums.PolylineColorMode.Selected)
            polyline.ZIndex = 5;
        else
            polyline.ZIndex = 1;

        return polyline;
    }

The PolylineColorMode is a quick enum to help me find out which polyline is selected and perform operations please ignore it.

My XAML:

<maps:MapControl x:Name="MyMap" extentions:PolylineCollection.PathCollection="{x:Bind ViewModel.PolylinePoints,Mode=OneWay}">

Where the extentions is a namespace where the above class (polylineCollection) resides.

The CodeBehind:

MapIcon errorIcon = new MapIcon()
            {
                Location = ViewModel.CurrentDriftLocation,
                NormalizedAnchorPoint = new Windows.Foundation.Point(0.5, 1.0),
                Title = "Drifted",
                ZIndex = 3,
                Image = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new System.Uri("ms-appx:///SharedAssets/ErrorAnalysysAssets/DriftPoint.png"))
            };
MyMap.MapElements.Add(errorIcon);

The ViewModelBacking property For Polyline

private ObservableCollection<Extentions.Map.Extentions.IPolylinePath> polylinePoints;
    public ObservableCollection<Extentions.Map.Extentions.IPolylinePath> PolylinePoints
    {
        get { return polylinePoints; }
        set { polylinePoints = value; RaisePropertyChanged(nameof(PolylinePoints)); }
    }

The IPolylinePath

public interface IPolylinePath
{
    SolidColorBrush PolylineColor { get; }

    int PolylineThinkness { get; set; }

    string PolylineTag { get; set; }

    IEnumerable<BasicGeoposition> PolylinePoints { get; set; }

    Geopath PolylineGeopath { get; }

    PolylineColorMode PolylineColorMode { get; set; }


}

Can anyone tell me why does this happen? or how can I handle it so that my mapControl can show other elements as well?

0

There are 0 answers