Click here to Skip to main content
Click here to Skip to main content

Google Map Distance Matrix and Geocoding API V3 web service intergration

, 13 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Integrating Google direction and geocoding services using C#. NET code behind.

Introduction 

Google Maps API V3 provides us different services like Direction, Distance Matrix, Geocoding etc. to integrate Google map features directly on to your web site. These services can be integrated many ways like front end (JavaScript) or back end (C#. NET code behind). So in this article I am going to discuss about integrating Google direction and geocoding services using C#. NET code behind.

Background     

I have been looking at some online articles or some online sample codes for Google Maps API v3 XML web service integration using C# code behind, but I could not find much information (may be my searching keywords didn't match with Google searching algorithm)  on the net, so i have decided to write a little article on my findings so far on my own research with Google Maps API v3.

Using the code  

Distance Matrix?

Service that Google offers for us to get a driving distance  from a post code to a post code. By default this service returns driving distance in KM, but if we customize request parameter like "units=imperial" we can get the driving distance in miles:

/// <summary>
/// returns driving distance in miles
/// </summary>
/// <param name="origin"></param>
/// <param name="destination"></param>
/// <returns></returns>
public static double GetDrivingDistanceInMiles(string origin, string destination)
{       
    string url = @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + 
      origin + "&destinations=" + destination + 
      "&mode=driving&sensor=false&language=en-EN&units=imperial";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    WebResponse response = request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    StreamReader sreader = new StreamReader(dataStream);
    string responsereader = sreader.ReadToEnd();
    response.Close();
    
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.LoadXml(responsereader);


    if (xmldoc.GetElementsByTagName("status")[0].ChildNodes[0].InnerText == "OK")
    {                                            
        XmlNodeList distance = xmldoc.GetElementsByTagName("distance");
        return Convert.ToDouble(distance[0].ChildNodes[1].InnerText.Replace(" mi", ""));
    }

    return 0;
}

Geocoding?

Service that Google offers for us to get geographic coordinates information like latitude and longitude.

/// <summary>
/// returns latitude 
/// </summary>
/// <param name="addresspoint"></param>
/// <returns></returns>
public double GetCoordinatesLat(string addresspoint)
{
    using (var client = new WebClient())
    {
        string seachurl = "http://maps.google.com/maps/geo?q='" + addresspoint + "'&output=csv";
        string[] geocodeInfo = client.DownloadString(seachurl).Split(',');
        return (Convert.ToDouble(geocodeInfo[2]));
    }
}

/// <summary>
/// returns longitude 
/// </summary>
/// <param name="addresspoint"></param>
/// <returns></returns>
public double GetCoordinatesLng(string addresspoint)
{
    using (var client = new WebClient())
    {
        string seachurl = "http://maps.google.com/maps/geo?q='" + addresspoint + "'&output=csv";
        string[] geocodeInfo = client.DownloadString(seachurl).Split(',');
        return (Convert.ToDouble(geocodeInfo[3]));
    }
}

Conclusion 

I hope this little article will help some one else in the community to get some very basic idea about integrating Google Maps API into their web site, please ask me any questions if there are and I will be happy to answer for it.

References  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

suis
Software Developer (Senior)
United Kingdom United Kingdom
having 10+ yrs of experience in software development.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberHenry Fatino28-May-14 9:45 
AnswerMessage Automatically Removed PinmemberAyesha Sharma20-Mar-14 1:32 
Question{Origins Destinations} Pinmemberms199-Mar-14 23:56 
GeneralMy vote of 5 PinprofessionalJas242-Dec-13 20:03 
QuestionAutomatic Routing with multiple weights Pinmemberbentroy19-Nov-13 2:00 
GeneralMy vote of 5 Pinmembercoder5221-Jul-13 20:13 
GeneralMy vote of 5 PinmemberPatrick Harris5-Jan-13 12:03 
GeneralRe: My vote of 5 Pinmembersuis7-Jan-13 0:51 
GeneralRe: My vote of 5 PinmemberPatrick Harris7-Jan-13 11:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 13 Dec 2012
Article Copyright 2012 by suis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid