在Windows Phone Map应用程序上使用JSON数据绘制折线

时间:2014-04-02 01:40:52

标签: c# json windows-phone-8 map

我在点击“绘制折线”按钮后,尝试使用JSON数据中的“PATH”绘制折线。但是我遇到了这个错误, “在ESRI.ArcGIS.Client.DLL中发生了'System.ArgumentException'类型的异常,但未在用户代码中处理 附加信息:无效的几何图形。“

我错过了任何密码吗?

    using ESRI.ArcGIS.Client;
    using ESRI.ArcGIS.Client.Geometry;
    using ESRI.ArcGIS.Client.Symbols;
    using ESRI.ArcGIS.Client.Tasks;
    using ESRI.ArcGIS.Client.Toolkit.DataSources;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Maps.Controls;
    using Microsoft.Phone.Shell;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Device.Location;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Navigation;
    using Test.Resources;



    namespace Test
    {
        public partial class MainPage : PhoneApplicationPage
        {

            private String finalPath;


            GraphicsLayer _myFromJsonGraphicsLayer;
            Draw _myDrawObject;

            // Constructor
            public MainPage()
            {
                InitializeComponent();


                _myFromJsonGraphicsLayer = MyMap.Layers["MyFromJsonGraphicsLayer"] as GraphicsLayer;


                _myDrawObject = new Draw(MyMap)
                {
                    LineSymbol = LayoutRoot.Resources["DrawLineSymbol"] as LineSymbol,
                    FillSymbol = LayoutRoot.Resources["DrawFillSymbol"] as FillSymbol
                };

                _myDrawObject.DrawComplete += MyDrawObject_DrawComplete;

            }

            private void MyDrawObject_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)
            {
                Graphic graphic = new Graphic()
                {
                    Geometry = args.Geometry,
                    Symbol = LayoutRoot.Resources["RedFillSymbol"] as FillSymbol
                };
                GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
                graphicsLayer.Graphics.Add(graphic);
            }

            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                //base.OnNavigatedTo(e);
                setUpLayers();

                // Create webclient.
                WebClient client = new WebClient();

                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
                //client.DownloadStringAsync(new Uri("http://www.onemap.sg/publictransportation/service1.svc/routesolns?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&sl="+startX+","+startY+"&el="+endX+","+endY+"&startstop=&endstop=&walkdist=300&mode=bus&routeopt=cheapest&retgeo=true&maxsolns=1&callback="));
                client.DownloadStringAsync(new Uri("http://www.onemap.sg/publictransportation/service1.svc/routesolns?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&sl=39167.4524,35518.8625&el=28987.5163,33530.5653&startstop=&endstop=&walkdist=300&mode=bus&routeopt=cheapest&retgeo=true&maxsolns=1&callback="));
            }

            private void setUpLayers()
            {
                ArcGISTiledMapServiceLayer baseMapLayer = new ArcGISTiledMapServiceLayer();
                baseMapLayer.ID = "BaseMap";
                baseMapLayer.Url = "http://e1.onemap.sg/arcgis/rest/services/SM128/MapServer";
                //baseMapLayer.Url = "http://onemap.sg/arcgis/rest/services/Basemap/MapServer";

                MyMap.Layers.Add(baseMapLayer);

            }

            public class STEP
            {
                //public string STEP { get; set; }
                public string type { get; set; }
                public string ServiceType { get; set; }
                public string ServiceID { get; set; }
                public string NumberOfStop { get; set; }
                public string BoardId { get; set; }
                public string BoardDesc { get; set; }
                public string BoardDist { get; set; }
                public string AlightId { get; set; }
                public string AlightDesc { get; set; }
                public string AlightDist { get; set; }
            }

            public class BusRoute
            {
                public string Solution { get; set; }
                public string Duration { get; set; }
                public string TotalCard { get; set; }
                public string TotalCash { get; set; }
                public string TotalDistance { get; set; }
                public List<STEP> STEPS { get; set; }
                public string TotalStops { get; set; }
                public List<List<string>> PATH { get; set; }
            }


            public class RootObject
            {
                public List<BusRoute> BusRoute { get; set; }
            }

            void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                // var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);

                JObject rawData = JObject.Parse(e.Result);
                String path = rawData["BusRoute"][0]["PATH"].ToString();
                String[] collectionOfPoints = path.Split(';');
                //JObject path = JObject.Parse(rawData["BusRoute"].ToString());

                finalPath = "";

                for (int i = 0; i < collectionOfPoints.Length; i++)
                {
                    if (i == 0)
                    {
                        finalPath = @"{""paths"":[" + collectionOfPoints[i] + "]";
                        finalPath = finalPath + ",";
                    }
                    else if (i == collectionOfPoints.Length - 1)
                    {
                        finalPath = finalPath + "[" + collectionOfPoints[i] + "],\"spatialReference\":{\"wkid\":4326}}";
                    }
                    else
                    {
                        finalPath = finalPath + "[" + collectionOfPoints[i] + "]";
                        finalPath = finalPath + ",";
                    }

                }
                tb_test.Text = finalPath;
            }


            private void DrawGeometryButton_Click(object sender, RoutedEventArgs e)
            {

                ESRI.ArcGIS.Client.Geometry.Geometry geometry = ESRI.ArcGIS.Client.Geometry.Geometry.FromJson(tb_test.Text);

                Graphic graphic = new Graphic();


                _myDrawObject.DrawMode = DrawMode.Polyline;
                tb_test.Text = finalPath;

            }
        }
    }

0 个答案:

没有答案