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

Tagged as

Consuming Google Maps API Rest Services in .NET

, 23 Jul 2010 CPOL
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.
 

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)

Share

About the Author

Pandey Vinay

India India
No Biography provided

Comments and Discussions

 
GeneralThank you for the fantastic example; voting it a 5!. It save... PinmemberMember 381091511-Dec-11 4:01 
GeneralI had actually submitted it as an article http://www.codepro... PinmemberPandey Vinay23-Jul-10 6:38 
GeneralRe: it sure is!, thank you very much :) Pinmemberbahman aminipour31-Oct-11 21:12 
GeneralThis isn't really a tip/trick. Why don't you flesh it out a... PinmemberTheyCallMeMrJames23-Jul-10 5:47 
GeneralFirst of all, make the tip readable. Format the code first. PinmemberArun Jacob22-Jul-10 4:08 
First of all, make the tip readable. Format the code first.

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

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

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