首页 > 代码库 > 代码片段 - C#基于google api计算距离
代码片段 - C#基于google api计算距离
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计算距离
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。