My name is Michal Sporek. I am a contractor in IT (software development). I am available for IT contracting jobs working remotely (telecommuting).

Please visit my site to learn more...

Using Google Maps API webservices in C# .NET

Google Maps API is a great way to integrate Google Maps data in your applications. There is a free developer license that you can use as long as long as you do not exceed 2500 requests per day - which is quite enough for simple applications meant for home usage to ease your daily life. If you are planning to send more than 2500 requests per day, you should consider purchasing the Google Maps API for Work license.

Google Maps API exposes webservices (https://developers.google.com/maps/documentation/webservices/) which you can consume in basically any programming language and platform that allows you to send a HTTP request on the Internet.

When developing C# .NET applications you can consume Google Maps API webservices in a number of ways, with two example ways given below:


The shortcoming of both those approaches is that when the results are returned from Google Maps API (either in XML, or in JSON format) you need to do quite a lot of parsing yourself to get the correct values from the fields that are in your interest. Also when sending the request to the webservices you need to build the query string yourself.

For the reasons above the best way to consume Google Maps API webservices in C# .NET is to use the Google Maps Web Services API wrapper for .NET (https://github.com/maximn/google-maps/). This excellent library makes it possible to use an Object-Oriented approach for working with Google Maps API webservices. The library is a wrapper that call Google Maps API internally and exposes it to you as a typed service. The library comes with a number of base types that you can use for constructing requests to be sent to the webservices, it provides types for responses that the webservices generate. The library also helps you with passing data to the webservices and with data conversions to make sure that the data that you send to the webservice are in the right format.

Below you can find a simple example of how to communicate with the Google Directions API ():

DirectionsRequest directionsRequest = new DirectionsRequest()
{
    Origin = "Surrey, UK",
    Destination = "London, UK",
}

DirectionsResponse directionsResponse = GoogleMapsApi.GoogleMaps.Directions.Query(directionsRequest);
GoogleMapsApi.Entities.Directions.Response.Leg leg = directionsResponse.Routes.ElementAt(0).Legs.ElementAt(0);
Console.WriteLine("Travelling from: {0}, {1}.", leg.StartAddress, leg.StartLocation);
Console.WriteLine("Travelling to: {0}, {1}.", leg.EndAddress, leg.EndLocation);
Console.WriteLine("The distance is: {0}, the travel will take {1}.", leg.Distance.Text, leg.Duration.Text);
foreach (var step in leg.Steps)
{
    Console.WriteLine(Regex.Replace(step.HtmlInstructions, @"<(.|\n)*?>", string.Empty));
}

This code will display the following information on the console:

Travelling from: Surrey, UK, 51.2588334,-0.4718817.
Travelling to: London, UK, 51.507235,-0.1280057.
The distance is: 47.6 km, the travel will take 52 mins.
Head northwest toward New RdPartial restricted usage road
Continue onto The St
Turn right onto Ripley Rd
Continue onto Hungry Hill Ln
Turn right onto Rose Ln
...
...


The Google Maps Web Services API wrapper for .NET is a robust library and it allows you to call Elevation API, Geocode API, Places API and TimeZone API as well. You get all the results from the webservices as .NET objects of concrete typed, which makes it very easy to build your .NET applications with it. It provides the same easiness of use that you have working with typed WCF services.

Posted by Michal Sporek