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

Consuming Google Maps API Rest Services in .NET

By , 23 Jul 2010
 
 

Introduction

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(
"http://maps.google.com/maps/api/geocode/xml?address={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();
response.Close();
outputStream.Close();
reader.Close();
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 =
(ElevationResponse)serializer.Deserialize(readerForDeserialization);
 

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.

License

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThank you for the fantastic example; voting it a 5!. It save...memberMember 381091511 Dec '11 - 3:01 
GeneralI had actually submitted it as an article http://www.codepro...memberPandey Vinay23 Jul '10 - 5:38 
I had actually submitted it as an article http://www.codeproject.com/KB/aspnet/GoogleMapsDataAPI.aspx but then I was asked to submit it as tip/trick.I am sure it would be useful to someone who is trying to access google api Rest Service and not the java script.
GeneralRe: it sure is!, thank you very much :)memberbahman aminipour31 Oct '11 - 20:12 
GeneralThis isn't really a tip/trick. Why don't you flesh it out a...memberTheyCallMeMrJames23 Jul '10 - 4:47 
GeneralFirst of all, make the tip readable. Format the code first.memberArun Jacob22 Jul '10 - 3:08 

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

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