using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { public class GMapUtil { public static List<directionsteps> GetDirections(string origin, string destination) { var requestUrl = string.Format("http://maps.google.com/maps/api/directions/xml?origin={0}&destination={1}&sensor=false", origin, destination); try { var client = new WebClient(); var result = client.DownloadString(requestUrl); return ParseDirectionResults(result); } catch (Exception) { return null; } } 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(); Label1.Text = 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; } } public class DirectionStep { public int Index { get; set; } public string Description { get; set; } public string Distance { get; set; } public string Duration { get; set; } } public class DirectionSteps { public string TotalDuration { get; set; } public string TotalDistance { get; set; } public string OriginAddress { get; set; } public string DestinationAddress { get; set; } public List<directionstep> Steps { get; set; } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)