地址之间的距离

时间:2012-12-22 01:20:39

标签: c# google-maps

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

5 个答案:

答案 0 :(得分:17)

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

注意:截至2018年6月11日,由于Google已停用对Maps API的无密钥访问权限,此方法将无法运行。如果您希望使用此方法,则必须注册其云平台并启用结算功能。

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

Distance Matrix serviceDirections-Service发送请求(当您需要路线距离时)

答案 3 :(得分: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;
    }

答案 4 :(得分:0)

我已经修复了上面答案中的代码,使其可以与Google Key一起使用。

  1. 获取Google地图的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;
    }