标签:
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 }
标签:
原文地址:http://www.cnblogs.com/philoyang/p/4180597.html