2012-12-22 17 views
8

Google Haritalar tarafından hesaplanan 2 adres arasında bir mesafe olması mümkün mü? Nasıl?Adresler arasındaki mesafe

+3

StackOverflow'a Hoş Geldiniz! Her türlü yardımı sağlayabilmemiz için çok daha fazla detay vermeniz gerekecek. Ne tür bir veri ile uğraşıyorsunuz? Ne denediniz (özellikle, ne tür bir kod yazdınız)? –

cevap

2

(bir rotanın mesafeyi gerektiğinde), adres dizeleri ya da API başlangıç ​​/ bitiş yerleri geçmesi Distance Matrix service veya Directions-Service için bir istek gönderin Koordinatlar ve Google tüm bacağınızı sizin için yapacak.

Yollar, belirttiğiniz kaç yol noktasına bağlı olarak çeşitli 'dan oluşur. Senaryosunda (0 yol noktası), sadece tahmini bir mesafe özelliğine sahip olan 1 ayağa sahip olmalısınız.

17

Sadece iki eklentiniz varsa, ilk olarak GEOCODING numaralı telefonu kullanın ve sonra aralarında mesafe elde etmek için birçok yöntem var.

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; 
    } 

VEYA

google ve ihtiyaç uğraşmak istemiyorsanız:

VEYA

Eğer coğrafi kodlama ihtiyacım yok ve eğer bir CS ÇÖZÜM Bu deneyin istiyorum sadece AIR DISTANCE, Bunu deneyin:

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 ayrıştırıcısını nereden bulabilirim – user1901860

+0

Ve fileGetContents fonlaması nedir? – user1901860

+0

JSon linq'i buradan indirdim: http://james.newtonking.com/pages/json-net.aspx – user1901860

0

Ve ben bunu yaptım: ancak boş bir dize döndürür. url = "http://maps.googleapis.com/maps/api/directions/json?origin=3320, rue de verdun, verdun & hedef = 379, 19e avenue, La Guadeloupe, G0M 1G0 & algılayıcı = 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; 
    }