8

有没有办法让谷歌地图计算出的两个地址之间的距离?如何?

4

5 回答 5

18

如果您只有两个地址,首先尝试通过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;
    }

或者

如果您不想与 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;
}

注意:自 2018 年 6 月 11 日起,此方法将不再有效,因为 Google 已禁用对 Maps API 的无密钥访问。如果您希望使用这种方法,则必须注册他们的云平台并启用计费。

于 2012-12-22T04:49:34.827 回答
2

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

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

于 2012-12-22T02:24:14.883 回答
1

我已经修复了上面答案中的代码,以便它可以与谷歌密钥一起使用。

  1. 获取谷歌地图的 API 密钥
  2. 安装 Nuget 包:Newtonsoft.Json

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;
    }
于 2019-07-02T02:28:46.057 回答
0

向距离矩阵服务方向服务发送请求(当您需要路线的距离时)

于 2012-12-22T02:16:39.360 回答
0

我这样做了:但它返回一个空字符串。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;
    }
于 2012-12-22T16:51:54.497 回答