2016-06-30 6 views
0

Seit langer Zeit versuche ich, eine Polylinie für Kreuzplattenform arbeiten zu machen. Ich habe es getan und es funktioniert gut, ich folgte der Map Control Tutorial zuerst, und dann Highlight a Route on a Map Tutorial.Xamarin Forms - Markieren Sie Route für WinPhone 8.1 Teil

Ich aktualisiere den Code, um es neu zu laden, wenn eine Änderungen kommt, aber ich bekomme ein Problem und ich konnte es nicht herausfinden ... Es funktioniert für Android & iOS.

polyline.Path = neuer Geopath (Koordinaten); wirft Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) Das Problem ist, dass meine beiden anderen Renderer (Android & iOS) funktioniert .. Vielleicht ist etwas nicht möglich, weil ich mit WinPhone8.1 arbeiten im Gegensatz zu dem Tutorial, das UWP ist.

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); 
     } 
    } 
} 

Ich habe auch gelesen, dass ein Schlüssel benötigt wird, so vielleicht ich nicht diese Schlüssel verwendet in einer guten Art und Weise .. Ich habe versucht, mit UWP Public Key und WinPhone8.X und früher Key, aber ohne Erfolg auch ..

Hat jemand eine Idee? Dieser Teil ein wirklich großes Problem in meiner App ..

Vielen Dank im Voraus!

Antwort

0

Nach einer langen Zeit, fand ich, warum es nicht funktioniert ...

Wenn Sie wie ich tun, wenn Sie einen HttpRequest etwas wie Google Richtung API tun, dann wird das Ergebnis kommt nach der ersten Passage in die OnElementChanged().

Da formsMap.RouteCoordinates ist nicht null aber leer, die Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) geworfen ..

Das ist die guteCustomMapRendererfür PolyLine Verwendung

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); 
      } 
     } 
    } 
}