2012-12-22 133 views
8

有沒有辦法讓Google地圖計算2個地址之間的距離? 如何?地址之間的距離

+3

歡迎來到StackOverflow!你將不得不提供更多的細節,以便我們提供任何形式的幫助。你處理什麼樣的數據?你有什麼想法(具體來說,你寫了什麼樣的代碼)? –

回答

2

您可以使用Google Directions API做到這一點,你傳遞的地址字符串或開始/結束位置的API請求座標和谷歌將爲你做所有的腿部工作。

路線由各種legs組成,具體取決於您指定的方式點數。在你的場景中(0方法點),你應該只有一條腿,它應該有一個估計的距離屬性。

17

如果你只是有兩個addreses首先嚐試得到lat lan由GEOCODING,然後有很多方法來獲得之間的距離。

OR如果你不需要地理編碼和

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

如果你不想惹谷歌和需要只有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; 
} 
+0

我在哪裏可以得到JObject解析器 – user1901860

+0

什麼是fileGetContents功能? – user1901860

+0

我在這裏下載了JSon linq: http://james.newtonking.com/pages/json-net.aspx – user1901860

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; 
    } 
相關問題