Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / C#
Tip/Trick

Consuming Google Maps API Rest Services in .NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
23 Jul 2010CPOL 35.1K   496   4   5
This is a direction to use Google Maps API Rest Services instead of using it from JavaScript.


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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThank you for the fantastic example; voting it a 5!. It save... Pin
Member 381091511-Dec-11 3:01
Member 381091511-Dec-11 3:01 
GeneralI had actually submitted it as an article http://www.codepro... Pin
Pandey Vinay23-Jul-10 5:38
Pandey 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 :) Pin
bahman aminipour31-Oct-11 20:12
bahman 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
TheyCallMeMrJames23-Jul-10 4:47 
GeneralFirst of all, make the tip readable. Format the code first. Pin
Arun Jacob22-Jul-10 3:08
Arun 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.