Distance between addresses

Is there a way to get the distance between two addresses calculated by Google Maps? How?

+8
c # google-maps
source share
5 answers

If you have only two addresses, first try to get local network information using GEOCODING, and then there are many ways to get the distance between them.

OR

if you don't need geocoding and want a CS solution, try this:

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

if you don’t want to contact Google and you only need AIR DISTANCE , try this:

 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. Starting June 11, 2018, this approach will no longer work, since Google has disabled keyless access to the Maps API. If you want to use this approach, you need to register on their cloud platform and enable billing.

+17
source share

You can do this using the Google Directions API , you pass start / end locations to the API in the form of address lines or coordinates, and Google will do all the work for you.

Routes consist of different legs depending on how many directions you indicate. In your scenario (0 way points) you should have only 1 foot, which should have the expected distance property.

+2
source share

Send a request for the Distance Matrix service or the Directions Service (if you need a distance to the route)

0
source share

And I did this: but it returns an empty string. url = "http://maps.googleapis.com/maps/api/directions/json?origin=3320, rue de verdun, verdun & destination = 379, prospectus 19e, 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; } 
0
source share

I fixed the code from the answer above so that it works with the Google key.

  1. Get API Key for Google Maps
  2. Install Nuget Package: 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; } 
0
source share

All Articles