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.