Bing Maps WPF控件不显示折线

时间:2017-02-17 19:36:55

标签: c# wpf xaml mvvm bing-maps

我的WPF MVVM应用程序中嵌入了Bing Maps WPF控件(纯ViewModel类被设置为整个视图的DataContext)。为了使Bing Map符合MVVM标准,我创建了一些附加属性,以便我可以在地图上显示路线。这里感兴趣的主要是MapLocations - 该应用程序使用BingMaps REST API来获取RoutePath的坐标列表。这是XAML:

<bing:Map
    classes:BingMap.RouteLineLayer="{Binding ElementName=RouteLineLayer}"
    classes:BingMap.Locations="{Binding MapLocations}" 
    classes:BingMap.Origin="{Binding MapOrigin}" 
    classes:BingMap.Destination="{Binding MapDestination}" 
    CredentialsProvider="{StaticResource BingCredentialsForRoyalFoods}">
    <bing:MapLayer x:Name="RouteLineLayer" />
</bing:Map>`

这是MapLocations和RouteLineLayer附加属性:

 public static readonly DependencyProperty LocationsProperty = DependencyProperty.RegisterAttached(
        "Locations", typeof (double[][]), typeof (BingMap), new PropertyMetadata(default(double[][]), LocationsChanged));

    private static void LocationsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var map = d as Map;

        if (map == null) return;

        map.Children.Clear();

        var routeLocations = e.NewValue as double[][];
        if (routeLocations == null) return;


        var locations = new LocationCollection();
        foreach (var routeLocation in routeLocations.Where(routeLocation => routeLocation.Length >= 2))
        {
            locations.Add(new Location(routeLocation[0], routeLocation[1]));
        }

        //Create a MapPolyline of the route and add it to the map
        var routeLine = new MapPolyline
        {
            Stroke = new SolidColorBrush(Colors.Blue),
            Locations = locations,
            StrokeThickness = 5,
            Width = 5
        };


        var routeLineLayer = GetRouteLineLayer(map);
        if (routeLineLayer == null)
        {
            routeLineLayer = new MapLayer();
            SetRouteLineLayer(map, routeLineLayer);
        }


        routeLineLayer.Children.Clear();
        routeLineLayer.Children.Add(routeLine);

        var rect = new LocationRect(routeLine.Locations[0], routeLine.Locations[routeLine.Locations.Count - 1]);
        map.SetView(rect);

    }

    public static void SetLocations(DependencyObject element, double[][] value)
    {
        element.SetValue(LocationsProperty, value);
    }

    public static double[][] GetLocations(DependencyObject element)
    {
        return (double[][]) element.GetValue(LocationsProperty);
    }

    public static readonly DependencyProperty RouteLineLayerProperty = DependencyProperty.RegisterAttached(
        "RouteLineLayer", typeof (MapLayer), typeof (BingMap), new PropertyMetadata(default(MapLayer), MapLayerChanged));

    private static void MapLayerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var map = d as Map;
        if (map == null) return;

        if (!map.Children.Contains((MapLayer)e.NewValue))
            map.Children.Add((MapLayer)e.NewValue);
    }

    public static void SetRouteLineLayer(DependencyObject element, MapLayer value)
    {
        element.SetValue(RouteLineLayerProperty, value);
    }

    public static MapLayer GetRouteLineLayer(DependencyObject element)
    {
        return (MapLayer) element.GetValue(RouteLineLayerProperty);
    }

我可以看到位置数组正在正确填充(我甚至尝试过对一些坐标进行硬编码)。无论如何,地图都不显示线路/路线。但它确实正确地限制了路线的起点和终点之间的地图。

非常感谢任何帮助。谢谢!

0 个答案:

没有答案