Hi,
Today i'll talking about how to get Distance in Meters between Address A and Address B using GoogleMaps and C#.
For this article i'll use Newtonsoft.Json library in order to Deserialize GoogleMaps JavaScript Response.
I'll call GoogleMaps directly by passing Address A and Address B in the Url et get GoogleMaps Response.
Here is the Sample using Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.IO;
using Newtonsoft.Json;
namespace GoogleDistanceCalculator
{
class Program
{
static void Main(string[] args)
{
String AddressA = String.Empty;
String AddressB = String.Empty;
Console.Write("Enter Address A : ");
AddressA = Console.ReadLine();
Console.Write("Enter Address B : ");
AddressB = Console.ReadLine();
StringBuilder sbUrl = new StringBuilder();
sbUrl.Append("http://maps.google.com/maps/nav?output=js&oe=utf8&q=");
sbUrl.AppendFormat("from:{0}", HttpUtility.UrlEncode(AddressA));
sbUrl.AppendFormat("+to:{0}", HttpUtility.UrlEncode(AddressB));
Uri googleUrl = new Uri(sbUrl.ToString());
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(googleUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
String gResponse = reader.ReadToEnd();
GoogleDirections directions = JavaScriptConvert.DeserializeObject(gResponse,
typeof(GoogleDirections)) as GoogleDirections;
Console.WriteLine("Distance : " +
directions.Directions.Distance.meters.ToString() + " Meters");
}
}
}
}
And here the GoogleMaps C# Implementation of the Response
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace GoogleDistanceCalculator
{
public class GeoCoordinate
{
public GeoCoordinate(double lat, double lon, int lev)
{
this.lat = lat;
this.lon = lon;
this.lev = lev;
}
public double lat;
public double lon;
public int lev;
}
public class Status
{
public Int64 code;
public string request;
}
public class Thoroughfare
{
public string ThoroughfareName;
}
public class PostalCode
{
public string PostalCodeNumber;
}
public class DependentLocality
{
public string DependentLocalityName;
public Thoroughfare Thoroughfare;
public PostalCode PostalCode;
}
public class Locality
{
public string LocalityName;
public Thoroughfare Thoroughfare;
public PostalCode PostalCode;
public DependentLocality DependentLocality;
}
public class SubAdministrativeArea
{
public string SubAdministrativeAreaName;
public Locality Locality;
}
public class AdministrativeArea
{
public string AdministrativeAreaName;
public SubAdministrativeArea SubAdministrativeArea;
public Locality Locality;
}
public class Country
{
public string CountryNameCode;
public string CountryName;
public AdministrativeArea AdministrativeArea;
}
public class Point
{
public double[] coordinates;
}
public class AddressDetails
{
public Country Country;
public Int64 Accuracy;
}
public class Placemark
{
public string id;
public string address;
public AddressDetails AddressDetails;
public Point Point;
}
public class Distance
{
public string meters;
public string html;
}
public class Duration
{
public string seconds;
public string html;
}
public class Directions
{
public string copyrightsHtml;
public string summaryHtml;
public Distance Distance;
public Duration Duration;
public Route[] Routes;
public Polyline Polyline;
}
public class Step
{
public string descriptionHtml;
public Distance Distance;
public Duration Duration;
public Point Point;
public Int64 polylineIndex;
}
public class Polyline
{
public string id;
public string points;
public string levels;
public Int64 numLevels;
public Int64 zoomFactor;
}
public class Route
{
public Distance Distance;
public Duration Duration;
public string summaryHtml;
public Step[] Steps;
public Point End;
public Int64 polylineEndIndex;
}
public class GoogleDirections
{
public string name;
public Status Status;
public Placemark[] Placemark;
public Directions Directions;
}
public class GoogleGeocodes
{
public string name;
public Status Status;
public Placemark[] Placemark;
}
}
Hope this will help :)
Views(2869)

