C# 地址之间的距离
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13999187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Distance between addresses
提问by user1901860
Is there a way to have a distance between 2 addresses calculated by Google Maps? How?
有没有办法让谷歌地图计算出两个地址之间的距离?如何?
回答by Dr.Molle
Send a request to the Distance Matrix serviceor to the Directions-Service(when you need the distance of a route)
回答by James
You can do this using the Google Directions API, you pass the start/end locations to the API as address strings or coordinates and Google will do all the leg work for you.
您可以使用Google Directions API执行此操作,您将开始/结束位置作为地址字符串或坐标传递给 API,Google 将为您完成所有的支线工作。
Routes are made up of various legsbased on how many way points you specify. In your scenario (0 way points) you should only have 1 leg which should have an estimated distance property.
根据您指定的路点数量,路线由不同的路段组成。在您的场景中(0 个航路点),您应该只有 1 条腿,它应该具有估计的距离属性。
回答by sajanyamaha
If you just have two addreses first try to get lat lan by GEOCODINGand then there are many methods to get distance in between.
如果您只有两个地址,请先尝试通过地理编码获取 lat lan ,然后有很多方法可以获取两者之间的距离。
OR
或者
if you dont need geocoding and want a CS SOLUTIONtry this :
如果您不需要地理编码并想要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;
}
OR
或者
if you dont want to mess with google and need only AIR DISTANCE,Try this :
如果你不想惹 google 并且只需要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;
}
NB: As of June 11th 2018 this approach will no longer work as Google has disabled keyless access to the Maps API. If you wish to use this approach you will have to sign up for their cloud platform and enable billing.
注意:自 2018 年 6 月 11 日起,此方法将不再有效,因为 Google 已禁用对 Maps API 的无密钥访问。如果您希望使用这种方法,则必须注册他们的云平台并启用计费。
回答by user1901860
And I did this: but it return an empty string. 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"
我这样做了:但它返回一个空字符串。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;
}
回答by Skorpi76
I have fixed the code from the answer above for it to work with google key.
我已经修复了上面答案中的代码,以便它可以与谷歌密钥一起使用。
- Get you API key for google maps
- Install Nuget package: Newtonsoft.Json
- 获取谷歌地图的 API 密钥
- 安装 Nuget 包:Newtonsoft.Json
3.
3.
public int getDistance(string origin, string destination)
{
System.Threading.Thread.Sleep(1000);
int distance = 0;
string key = "YOUR KEY";
string url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&key=" + key;
url = url.Replace(" ", "+");
string content = fileGetContents(url);
JObject o = JObject.Parse(content);
try
{
distance = (int)o.SelectToken("routes[0].legs[0].distance.value");
return distance;
}
catch
{
return distance;
}
}
protected string fileGetContents(string fileName)
{
string sContents = string.Empty;
string me = string.Empty;
try
{
if (fileName.ToLower().IndexOf("https:") > -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;
}

