如何根据JSON数据在地图上显示折线和标记? (Windows Phone)

时间:2014-03-31 06:34:42

标签: json windows-phone-8 maps polyline

我可以在地图上绘制由MapGesture事件点击的区域上的折线,但是我想根据我的JSON数据显示折线。有没有人可以给我一些指导我如何编码呢?谢谢!

        public MainPage()
    {
        InitializeComponent();


        Draw MyDrawObject = new Draw(MyMap)
        {
            FillSymbol = LayoutRoot.Resources["RedFillSymbol"] as FillSymbol
        };

        MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;

        MyDrawObject.DrawMode = DrawMode.Polyline;
        MyDrawObject.IsEnabled = true;


        // 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=530927&el=569830&startstop=&endstop=&walkdist=300&mode=bus&routeopt=cheapest&retgeo=true&maxsolns=1&callback="));

    }

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

    private void setUpLayers()
    {
        ArcGISTiledMapServiceLayer baseMapLayer = new ArcGISTiledMapServiceLayer();
        baseMapLayer.ID = "BaseMap";
        baseMapLayer.Url = "http://e1.onemap.sg/arcgis/rest/services/SM128/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);
        foreach (var route in rootObject.BusRoute)
        {
            string duration = route.Duration;
            string totalStops = route.TotalStops;
            //tb_test.Text = "Total Duration: " + duration;
            //tb_test2.Text = "Total number of stops: " + totalStops;
        }
    }


    private void MyMap_MapGesture(object sender, ESRI.ArcGIS.Client.Map.MapGestureEventArgs e)
    {
        if(e.Gesture == GestureType.Tap)
        {
            MapPoint point = new MapPoint(e.MapPoint.X,e.MapPoint.Y);

            double x = point.X;
            double y = point.Y;

             var _polyline = new MapPolyline();
             _polyline.StrokeColor = Colors.Blue;
             _polyline.StrokeThickness = 2;
            _polyline.Path.Add(new GeoCoordinate(x,y));

        }
    }

}

1 个答案:

答案 0 :(得分:0)

您需要有一个webapi(或任何服务)端点来连接和检索数据。获得数据后,您需要将DataTransfer对象转换为GeoCoordinate对象,以便将其添加到地图中:

MapPolyline line = new MapPolyline();

public void InsertCoordsToMap(IEnumerable<YourDtoClass> deSerializedJson) {
foreach(var item in deSerializedJson) {
    GeoCoordinate elementToAdd = new GeoCoordinate(item.Latidude, item.Longitude);
    map.Path.Add(elementToAdd);
}

}