Click here to Skip to main content
15,885,216 members
Articles / Web Development / HTML
Tip/Trick

Google Map Distance Matrix and Geocoding API V3 web service intergration

Rate me:
Please Sign up or sign in to vote.
4.91/5 (8 votes)
13 Dec 2012CPOL1 min read 77.4K   20   10
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:

C#
/// <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.

C#
/// <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)


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

Comments and Discussions

 
QuestionError Pin
Member 1436881414-May-19 14:51
Member 1436881414-May-19 14:51 
GeneralMy vote of 5 Pin
Henry Fatino28-May-14 9:45
Henry Fatino28-May-14 9:45 
Question{Origins Destinations} Pin
ms199-Mar-14 23:56
ms199-Mar-14 23:56 
GeneralMy vote of 5 Pin
Tom Marvolo Riddle2-Dec-13 20:03
professionalTom Marvolo Riddle2-Dec-13 20:03 
QuestionAutomatic Routing with multiple weights Pin
bentroy19-Nov-13 2:00
bentroy19-Nov-13 2:00 
GeneralMy vote of 5 Pin
coder5221-Jul-13 20:13
coder5221-Jul-13 20:13 
GeneralMy vote of 5 Pin
Patrick Harris5-Jan-13 12:03
Patrick Harris5-Jan-13 12:03 
GeneralRe: My vote of 5 Pin
suis7-Jan-13 0:51
suis7-Jan-13 0:51 
GeneralRe: My vote of 5 Pin
Patrick Harris7-Jan-13 11:59
Patrick Harris7-Jan-13 11:59 

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

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