Click here to Skip to main content
13,191,638 members (65,108 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 23 Jul 2010

Consuming Google Maps API Rest Services in .NET

, 23 Jul 2010
Rate this:
Please Sign up or sign in to vote.
This is a direction to use Google Maps API Rest Services instead of using it from JavaScript.


GooglMapsAPI is very easy to use if you have to use it on an HTML page using JavaScript, but if you have to use GooglMapsAPI for retrieving data instead of directly displaying the page, then it becomes a bit hectic. The output is available in two forms JSON and XML. The attached code contains a class that can be used to deserialize the XML output and format it in .NET class so that we can use it the way we want.

Using the code

For using the GoogleMapsAPI, what we do is follow:

First make formatted HTTP request to the service.

string url =String.Format(
"{0}&sensor=false", "Andheri West".Replace(" ", ",")
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url );
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream outputStream = response.GetResponseStream();
StreamReader reader = new StreamReader(outputStream, Encoding.ASCII);
string output = reader.ReadToEnd();

The response is either in the form on JSON or XML so deserialise the response into a .NET class.
Once we have the response of our request, it is time to deserialize the response:
XmlSerializer serializer = new XmlSerializer(typeof(ElevationResponse));
streamForDeserialization streamForDeserialization = new StringReader(output);
XmlTextReader readerForDeserialization = new XmlTextReader(streamForDeserialization);  
ElevationResponse elevationResponse =

Use the output as you need. Since the output is not easy to understand, it is better is to find out the neccessary fields and use them instead of the object of entire response.


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


About the Author

Pandey Vinay
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralThank you for the fantastic example; voting it a 5!. It save... Pin
Member 381091511-Dec-11 3:01
memberMember 381091511-Dec-11 3:01 
Thank you for the fantastic example; voting it a 5!. It saved me lots of time. In appreciation and to the community, I'm attaching a demonstrative snippet showing a simple way of parsing out values from the GeoCodeResponse XML result.

<pre lang="c#">
if (geocodeResponse.Status == "OK")
for (int i = 0; i < geocodeResponse.Result[0].AddressComponent.GetLength(0); i++)
string tmp = geocodeResponse.Result[0].AddressComponent[i].Type[0].Value;
if (tmp == "street_number")
gAddress1.Text = geocodeResponse.Result[0].AddressComponent[i].LongName + " ";
else if (tmp == "route")
gAddress1.Text = gAddress1.Text + geocodeResponse.Result[0].AddressComponent[i].LongName;
else if (tmp == "locality")
gCity.Text = geocodeResponse.Result[0].AddressComponent[i].LongName;
else if (tmp == "administrative_area_level_1")
gState.Text = geocodeResponse.Result[0].AddressComponent[i].ShortName; // 2 letter state
else if (tmp == "postal_code")
gPostalCode.Text = geocodeResponse.Result[0].AddressComponent[i].LongName;
Lit.Text = geocodeResponse.Result[0].FormattedAddress;
// Sometimes the "sublocality" (verses "locality") is correct and other times not.
// Example are New York City addresses; Brooklyn and NYC
// Compare the two and if different set the City from the Formatted Address
string[] addrSplit = Lit.Text.Split(',');
if (gCity.Text.Trim().ToLower() != addrSplit[1].Trim().ToLower())
gCity.Text = addrSplit[1].Trim();
Lattitude.Text = geocodeResponse.Result[0].Geometry[0].Location[0].Lattitude;
Longitude.Text = geocodeResponse.Result[0].Geometry[0].Location[0].Longitude;
viewpointSWLat.Text = geocodeResponse.Result[0].Geometry[0].Viewport[0].SouthWest[0].Lattitude;
viewpointSWLon.Text = geocodeResponse.Result[0].Geometry[0].Viewport[0].SouthWest[0].Longitude;
viewpointNELat.Text = geocodeResponse.Result[0].Geometry[0].Viewport[0].NorthEast[0].Lattitude;
viewpointNELon.Text = geocodeResponse.Result[0].Geometry[0].Viewport[0].NorthEast[0].Longitude;
gCompare.Text = String.Format("{0}", geocodeFailsCompare());
GeneralI had actually submitted it as an article http://www.codepro... Pin
Pandey Vinay23-Jul-10 5:38
memberPandey Vinay23-Jul-10 5:38 
GeneralRe: it sure is!, thank you very much :) Pin
bahman aminipour31-Oct-11 20:12
memberbahman aminipour31-Oct-11 20:12 
GeneralThis isn't really a tip/trick. Why don't you flesh it out a... Pin
TheyCallMeMrJames23-Jul-10 4:47
memberTheyCallMeMrJames23-Jul-10 4:47 
GeneralFirst of all, make the tip readable. Format the code first. Pin
Arun Jacob22-Jul-10 3:08
memberArun Jacob22-Jul-10 3:08 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171017.2 | Last Updated 23 Jul 2010
Article Copyright 2010 by Pandey Vinay
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid