问题理解代码!! [英] Problem to understand the code!!

查看:122
本文介绍了问题理解代码!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过搜索google找到了代码...以获取指示.. pls 请有人可以向我解释一下吗?....


I have found a code by searching google... for get direction.. so pls Please can someone explain me?....


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

推荐答案

顺便说一句,

我认为这些可以帮助您:

我如何理解开源项目代码?

C#.NET中的断点
By the way,

I think these can help you:

How do I understand an open source project code?

Breakpoints in C# .NET


这篇关于问题理解代码!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆