예제 #1
0
        public override BasicRoute ParseResult(string response)
        {
            // Parse the Result (it is JSON)
            JsonObject jsonObject = JsonObject.Parse(response);

            // Create a new Route
            BasicRoute Route = new BasicRoute();


            IJsonValue nameJsonValue = jsonObject.GetNamedValue(nameKey);

            if (nameJsonValue.ValueType == JsonValueType.Null)
            {
                Route.Name = "New Route"; // TODO: Give it a name!
            }
            else
            {
                Route.Name = nameJsonValue.GetString();
            }



            IJsonValue distanceJsonValue = jsonObject.GetNamedValue(distanceKey);

            if (distanceJsonValue.ValueType == JsonValueType.Null)
            {
                Route.Distance = null; // TODO: If there is no distance, calculate it ourselves!
            }
            else
            {
                Route.Distance = distanceJsonValue.GetNumber();
            }



            IJsonValue upHillJsonValue = jsonObject.GetNamedValue(uphillKey);

            if (upHillJsonValue.ValueType == JsonValueType.Null)
            {
                Route.Uphill = null; // Null means no info in this case
            }
            else
            {
                Route.Uphill = upHillJsonValue.GetNumber();
            }



            IJsonValue downHillJsonValue = jsonObject.GetNamedValue(downhillKey);

            if (downHillJsonValue.ValueType == JsonValueType.Null)
            {
                Route.Downhill = null; // Null means no info in this case
            }
            else
            {
                Route.Downhill = downHillJsonValue.GetNumber();
            }


            JsonObject startPointJsonObject = jsonObject.GetNamedObject(startPointKey);

            Route.StartPoint = new BasicLocation()
            {
                Location = new Geopoint(new BasicGeoposition()
                {
                    Latitude  = startPointJsonObject.GetNamedNumber("lat", Waypoints.ElementAt(0).Location.Position.Latitude), // Just use the info we already have if there is none
                    Longitude = startPointJsonObject.GetNamedNumber("lng", Waypoints.ElementAt(0).Location.Position.Longitude),
                    Altitude  = startPointJsonObject.GetNamedNumber("alt", Waypoints.ElementAt(0).Location.Position.Altitude)
                })
            };


            List <BasicGeoposition> Track = new List <BasicGeoposition>();

            foreach (IJsonValue jsonValue in jsonObject.GetNamedArray(trackKey, new JsonArray()))
            {
                if (jsonValue.ValueType == JsonValueType.Object)
                {
                    JsonObject jsonValueAsObject = jsonValue.GetObject();
                    Track.Add(new BasicGeoposition()
                    {
                        Latitude  = jsonValueAsObject.GetNamedNumber("lat"),
                        Longitude = jsonValueAsObject.GetNamedNumber("lng"),
                        Altitude  = jsonValueAsObject.GetNamedNumber("alt")
                    });
                }
            }
            Route.Track = Track;



            return(Route);
        }