Xamarin Forms - WinPhone 8.1 部分的高亮路线

Xamarin Forms - Highlight route for WinPhone 8.1 part

很长一段时间以来,我都在尝试将作品制作成折线以供crossplateform使用。 我做到了,效果很好,我遵循了 Map Control tutorial in first, and then Highlight a Route on a Map 教程。

然后我更新代码以使其在发生任何更改时重新加载,但是,我遇到了一个问题,我无法弄清楚...它确实适用于 Android & iOS.

polyline.Path = new Geopath(坐标); 抛出 Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) 问题是我的另外两个渲染器 (Android & iOS) 工作..也许有些事情是不可能的,因为我使用的是 WinPhone8.1,不像教程,它是 UWP。

public class CustomMapRenderer : MapRenderer
{
    MapControl nativeMap;
    CustomMap formsMap;

    protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
            nativeMap = Control as MapControl;
        }

        if (e.NewElement != null)
        {
            formsMap = (CustomMap)e.NewElement;
            nativeMap = Control as MapControl;

            UpdatePolyLine();
        }
    }

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (this.Element == null || this.Control == null)
            return;

        if (e.PropertyName == CustomMap.RouteCoordinatesProperty.PropertyName)
        {
            UpdatePolyLine();
        }
    }

    private void UpdatePolyLine()
    {
        if (nativeMap != null)
        {
            var coordinates = new List<BasicGeoposition>();
            foreach (var position in formsMap.RouteCoordinates)
            {
                coordinates.Add(new BasicGeoposition() { Latitude = position.Latitude, Longitude = position.Longitude });
            }

            var polyline = new MapPolyline();
            polyline.StrokeColor = Color.FromArgb(128, 255, 0, 0);
            polyline.StrokeThickness = 5;
            polyline.Path = new Geopath(coordinates);
            nativeMap.MapElements.Add(polyline);
        }
    }
}

我还读到需要 key,所以也许我没有很好地使用这个密钥。我尝试使用 UWP Public Key 和WinPhone8.X 和更早的密钥,但也没有成功..

有人有想法吗?这部分在我的应用程序中是一个非常大的问题..

提前致谢!

弄了半天,发现为什么不行了...

如果你像我一样,如果你对Google方向APIHttpRequest,那么结果将在 OnElementChanged().

的第一段之后出现

因为 formsMap.RouteCoordinates 不是 null 而是 ,所以抛出 Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))..

这是好的 CustomMapRenderer for PolyLine use

using PROJECT;
using PROJECT.UWP;
using System.Collections.Generic;
using Windows.Devices.Geolocation;
using Windows.UI.Xaml.Controls.Maps;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Maps.UWP;
using Xamarin.Forms.Platform.UWP;

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace PROJECT.UWP
{
    public class CustomMapRenderer : MapRenderer
    {
        MapControl nativeMap;
        CustomMap formsMap;

        protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                nativeMap = Control as MapControl;
            }

            if (e.NewElement != null)
            {
                formsMap = (CustomMap)e.NewElement;
                nativeMap = Control as MapControl;
                UpdatePolyLine();
            }
        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (this.Element == null || this.Control == null)
                return;

            if (e.PropertyName == CustomMap.RouteCoordinatesProperty.PropertyName)
            {
                UpdatePolyLine();
            }
        }

        private void UpdatePolyLine()
        {
            if (formsMap != null && formsMap.RouteCoordinates.Count > 0)
            {
                List<BasicGeoposition> coordinates = new List<BasicGeoposition>();

                foreach (var position in formsMap.RouteCoordinates)
                {
                    coordinates.Add(new BasicGeoposition() { Latitude = position.Latitude, Longitude = position.Longitude });
                }

                Geopath path = new Geopath(coordinates);
                MapPolyline polyline = new MapPolyline();
                polyline.StrokeColor = Windows.UI.Color.FromArgb(128, 255, 0, 0);
                polyline.StrokeThickness = 5;
                polyline.Path = path;
                nativeMap.MapElements.Add(polyline);
            }
        }
    }
}