Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
I have found a code by searching google... for get direction.. so pls Please can someone explain me?....


C#
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; }
}
    }
}
Posted
Updated 30-Jun-13 8:47am
v2
Comments
Thanks7872 30-Jun-13 13:48pm    
Dont repost questions like this.
[no name] 30-Jun-13 13:51pm    
Not only is this just a code dump but it is also a reposting. But to answer you plainly so you can stop doing this, no, no one is going to sit down and explain this code to you line by line. It would take far too long and there is no benefit for anyone to try and teach you programming in a forum posting.
Shahin Khorshidnia 30-Jun-13 14:45pm    
And also
1. Please do not ask your question in Subject TextBox.
2. You have enough time to type words completely, then please use 'Please' instead of 'pls'
promod madushan 30-Jun-13 23:25pm    
okay okay!!!
but i just want List<directionsteps>.... this expanation only......

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900