Ejemplo n.º 1
0
        private static List<DirectionSteps> ParseDirectionResults(string result)
        {
            var directionStepsList = new List<DirectionSteps>();
            var xmlDoc = new XmlDocument { InnerXml = result };
            if (xmlDoc.HasChildNodes)
            {
                var directionsResponseNode = xmlDoc.SelectSingleNode("DirectionsResponse");
                if (directionsResponseNode != null)
                {
                    var statusNode = directionsResponseNode.SelectSingleNode("status");
                    if (statusNode != null && statusNode.InnerText.Equals("OK"))
                    {
                        var legs = directionsResponseNode.SelectNodes("route/leg");
                        foreach (XmlNode leg in legs)
                        {
                            int stepCount = 1;
                            var stepNodes = leg.SelectNodes("step");
                            var steps = new List<DirectionStep>();
                            foreach (XmlNode stepNode in stepNodes)
                            {
                                var directionStep = new DirectionStep();
                                directionStep.Index = stepCount++;
                                directionStep.Distance = stepNode.SelectSingleNode("distance/text").InnerText;
                                directionStep.Duration = stepNode.SelectSingleNode("duration/text").InnerText;

                                directionStep.Description = Regex.Replace(stepNode.SelectSingleNode("html_instructions").InnerText, "<[^<]+?>", "");
                                steps.Add(directionStep);
                            }

                            var directionSteps = new DirectionSteps();
                            directionSteps.OriginAddress = leg.SelectSingleNode("start_address").InnerText;
                            directionSteps.DestinationAddress = leg.SelectSingleNode("end_address").InnerText;
                            directionSteps.TotalDistance = leg.SelectSingleNode("distance/text").InnerText;
                            directionSteps.TotalDuration = leg.SelectSingleNode("duration/text").InnerText;
                            directionSteps.Steps = steps;

                            directionStepsList.Add(directionSteps);
                        }
                    }
                }
            }
            return directionStepsList;
        }
Ejemplo n.º 2
0
        private static List <DirectionSteps> ParseDirectionResults(string result, Models.Coordinate origin, Models.Coordinate destination, List <Models.Coordinate> waypoints, bool avoidHighways, bool avoidTolls, int unitSystem, string travelMode)
        {
            var directionStepsList = new List <DirectionSteps>();

            var xmlDoc = new XmlDocument {
                InnerXml = result
            };

            if (xmlDoc.HasChildNodes)
            {
                var directionsResponseNode = xmlDoc.SelectSingleNode("DirectionsResponse");

                var statusNode = directionsResponseNode?.SelectSingleNode("status");

                if (statusNode != null && statusNode.InnerText.Equals("OK"))
                {
                    var legs = directionsResponseNode.SelectNodes("route/leg");

                    if (legs == null)
                    {
                        return(directionStepsList);
                    }

                    foreach (XmlNode leg in legs)
                    {
                        int stepCount = 1;
                        var stepNodes = leg.SelectNodes("step");
                        var steps     = (from XmlNode stepNode in stepNodes
                                         select new DirectionStep
                        {
                            Index = stepCount++,
                            Distance = Convert.ToDouble(stepNode.SelectSingleNode("distance/value").InnerText, System.Globalization.CultureInfo.InvariantCulture),
                            DistanceText = stepNode.SelectSingleNode("distance/text").InnerText,
                            Duration = Convert.ToDouble(stepNode.SelectSingleNode("duration/value").InnerText, System.Globalization.CultureInfo.InvariantCulture),
                            DurationText = stepNode.SelectSingleNode("duration/text").InnerText,
                            Description = WebUtility.HtmlDecode(stepNode.SelectSingleNode("html_instructions").InnerText)
                        }).ToList();

                        var directionSteps = new DirectionSteps
                        {
                            OriginPoint = new Models.Coordinate
                            {
                                SequenceNo = -1,
                                Latitude   = Convert.ToDouble(leg.SelectSingleNode("start_location/lat").InnerText, System.Globalization.CultureInfo.InvariantCulture),
                                Longitude  = Convert.ToDouble(leg.SelectSingleNode("start_location/lng").InnerText, System.Globalization.CultureInfo.InvariantCulture)
                            },
                            OriginAddress    = leg.SelectSingleNode("start_address").InnerText,
                            DestinationPoint = new Models.Coordinate
                            {
                                SequenceNo = -1,
                                Latitude   = Convert.ToDouble(leg.SelectSingleNode("end_location/lat").InnerText, System.Globalization.CultureInfo.InvariantCulture),
                                Longitude  = Convert.ToDouble(leg.SelectSingleNode("end_location/lng").InnerText, System.Globalization.CultureInfo.InvariantCulture)
                            },
                            DestinationAddress = leg.SelectSingleNode("end_address").InnerText,
                            TotalDistance      = Convert.ToDouble(leg.SelectSingleNode("distance/value").InnerText, System.Globalization.CultureInfo.InvariantCulture),
                            TotalDistanceText  = leg.SelectSingleNode("distance/text").InnerText,
                            TotalDuration      = Convert.ToDouble(leg.SelectSingleNode("duration/value").InnerText, System.Globalization.CultureInfo.InvariantCulture),
                            TotalDurationText  = leg.SelectSingleNode("duration/text").InnerText,
                            Steps = steps
                        };

                        directionStepsList.Add(directionSteps);
                    }
                }
                else if (statusNode != null && statusNode.InnerText.Equals("OVER_QUERY_LIMIT"))
                {
                    Thread.Sleep(1000);
                    directionStepsList = GetDirections(origin, destination, waypoints, avoidHighways, avoidTolls, unitSystem, travelMode);
                }
                else
                {
                    throw new NotSupportedException(statusNode?.InnerText);
                }
            }
            return(directionStepsList);
        }