地址之间的距离

有没有办法让谷歌地图计算出2个地址之间的距离? 怎么样?

如果你只有两个地址,首先尝试通过GEOCODING获得lat lan,然后有很多方法来获得它们之间的距离。

要么

如果你不需要地理编码并想要一个CS解决方案试试这个:

public int getDistance(string origin, string destination) { System.Threading.Thread.Sleep(1000); int distance = 0; //string from = origin.Text; //string to = destination.Text; string url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false"; string requesturl = url; //string requesturl = @"http://maps.googleapis.com/maps/api/directions/json?origin=" + from + "&alternatives=false&units=imperial&destination=" + to + "&sensor=false"; string content = fileGetContents(requesturl); JObject o = JObject.Parse(content); try { distance = (int)o.SelectToken("routes[0].legs[0].distance.value"); return distance; } catch { return distance; } return distance; //ResultingDistance.Text = distance; } protected string fileGetContents(string fileName) { string sContents = string.Empty; string me = string.Empty; try { if (fileName.ToLower().IndexOf("http:") > -1) { System.Net.WebClient wc = new System.Net.WebClient(); byte[] response = wc.DownloadData(fileName); sContents = System.Text.Encoding.ASCII.GetString(response); } else { System.IO.StreamReader sr = new System.IO.StreamReader(fileName); sContents = sr.ReadToEnd(); sr.Close(); } } catch { sContents = "unable to connect to server "; } return sContents; } 

要么

如果你不想搞乱谷歌并且只需要AIR DISTANCE ,试试这个:

 public decimal calcDistance(decimal latA, decimal longA, decimal latB, decimal longB) { double theDistance = (Math.Sin(DegreesToRadians(latA)) * Math.Sin(DegreesToRadians(latB)) + Math.Cos(DegreesToRadians(latA)) * Math.Cos(DegreesToRadians(latB)) * Math.Cos(DegreesToRadians(longA - longB))); return Convert.ToDecimal((RadiansToDegrees(Math.Acos(theDistance)))) * 69.09M * 1.6093M; } 

您可以使用Google Directions API执行此操作,将起始/结束位置作为地址字符串或坐标传递给API,Google将为您完成所有工作。

根据您指定的路点数,路线由各种路段组成 。 在您的场景中(0路点),您应该只有一条腿应具有估计的距离属性。

向Distance Matrix服务或Directions-Service发送请求(当您需要路线的距离时)

我做了这个:但它返回一个空字符串。 url =“http://maps.googleapis.com/maps/api/directions/json?origin=3320,rue de verdun,verdun&destination = 379,19e avenue,La Guadeloupe,G0M 1G0&sensor = false”

 public string fileGetContents(string url) { string text = ""; var webRequest = HttpWebRequest.Create(url); IAsyncResult asyncResult = null; asyncResult = webRequest.BeginGetResponse( state => { var response = webRequest.EndGetResponse(asyncResult); using (var sr = new StreamReader(response.GetResponseStream())) { text = sr.ReadToEnd(); } }, null ); return text; }