码迷,mamicode.com
首页 > Windows程序 > 详细

代码片段 - C#基于google api计算距离

时间:2014-12-23 19:10:36      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

 1 /// <summary>
 2 /// returns driving distance in kilometers
 3 /// </summary>
 4 /// <param name="origin"></param>
 5 /// <param name="destination"></param>
 6 /// <returns></returns>
 7 public static double GetDrivingDistanceInKilometers(string origin, string destination)
 8 { 
 9 string url = @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + 
10 origin + "&destinations=" + destination + 
11 "&mode=driving&sensor=false&language=en-EN&units=imperial";
12 
13 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
14 WebResponse response = request.GetResponse();
15 Stream dataStream = response.GetResponseStream();
16 StreamReader sreader = new StreamReader(dataStream);
17 string responsereader = sreader.ReadToEnd();
18 response.Close();
19 
20 XmlDocument xmldoc = new XmlDocument();
21 xmldoc.LoadXml(responsereader);
22 
23 
24 if (xmldoc.GetElementsByTagName("status")[0].ChildNodes[0].InnerText == "OK")
25 { 
26 XmlNodeList distance = xmldoc.GetElementsByTagName("distance");
27 var miledistance = Convert.ToDouble(distance[0].ChildNodes[1].InnerText.Replace(" mi", ""));
28 var kmdistance = (miledistance * 1.609344).toFixed(1);
29 return kmdistance;
30 }
31 
32 return 0;
33 }

 

代码片段 - C#基于google api计算距离

标签:

原文地址:http://www.cnblogs.com/philoyang/p/4180597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!