C# 地址之间的距离
有没有办法让谷歌地图计算出两个地址之间的距离? 如何发送?向或发送请求(当您需要路线的距离时)您可以使用,将起始/结束位置作为地址字符串或坐标传递给API,Google将为您完成所有分支工作C# 地址之间的距离,c#,google-maps,C#,Google Maps,有没有办法让谷歌地图计算出两个地址之间的距离? 如何发送?向或发送请求(当您需要路线的距离时)您可以使用,将起始/结束位置作为地址字符串或坐标传递给API,Google将为您完成所有分支工作 根据指定的路径点的数量,路线由各种路线组成。在您的场景(0个路径点)中,您应该只有一个具有估计距离属性的支腿。如果您只有两个addrese,请首先尝试通过获取lat lan,然后有许多方法来获取两者之间的距离 或 如果您不需要地理编码,并且想要一个CS解决方案,请尝试以下方法: public int get
根据指定的路径点的数量,路线由各种路线组成。在您的场景(0个路径点)中,您应该只有一个具有估计距离属性的支腿。如果您只有两个addrese,请首先尝试通过获取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;
}
或
如果您不想与谷歌为伍,只需要空中距离,请尝试以下方法:
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:截至2018年6月11日,这种方法将不再有效,因为谷歌已禁用对地图API的无钥匙访问。如果您希望使用这种方法,您必须注册他们的云平台并启用计费。我这样做了:但它返回一个空字符串。 url=”http://maps.googleapis.com/maps/api/directions/json?origin=3320,凡尔登街,凡尔登&目的地=379,瓜德罗普岛19e大道,G0M 1G0&传感器=false”
我已经修复了上面答案中的代码,以便它与google key一起工作
欢迎来到StackOverflow!你必须提供更多的细节,以便我们提供任何形式的帮助。你在处理什么样的数据?您尝试过什么(具体地说,您编写了什么类型的代码)?对于任何寻求性能解决方案的人,我建议使用来计算您知道纬度和经度的两个位置之间的距离。下面使用Google Directions API的解决方案a)在某些情况下不会返回正确答案,因为行驶距离可能与实际距离不同;b)调用Directions API可能需要一秒钟左右的时间。此外,c)由于Maps改变了其定价政策,这可能要花很多钱。我在哪里可以得到JObject解析器,fileGetContents函数是什么?我在这里下载了JSon linq:@sajanyamaha我用了你的代码,效果很好。但是如果我给出了不存在的地址,它仍然会计算距离,如果我在谷歌地图上查看,它会说“找不到地址”,如何实现这一点。谢谢你的代码。请注意,这段代码将不再工作,因为你现在需要一个API密钥。
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;
}
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;
}