I'm writing an asp.net program in C# that should take an address, use the google geocoding web service to get back an XML page, and then find the longitude and latitude of that address on the XML page. If I have built the query string (for example http://maps.googleapis.com/maps/api/geocode/xml?address=1000+Fifth+Avenue,+New+York,+NY&sensor=false), how would I go about sending that query and getting back the return XML page in C#?
1 Answer
Do this;
XmlDocument xDoc = new XmlDocument();
xDoc.Load("http://maps.googleapis.com/maps/api/geocode/xml?address=1000+Fifth+Avenue,+New+York,+NY&sensor=false");
This'll give you your XmlDocument and then you can process this as you wish.
So if you wanted to obtain the lat and long, you could use XPath
var lat = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
var longitude = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;
5 Comments
Graham Carling
Thanks, this gets me the lat and long perfectly, I really appreciate the help.
saj
Glad it helps, feel free to mark the question as answered by selecting the tick next to the answer. Thanks
Graham Carling
Sorry, I worded it horribly and I'm trying to say this more clearly so I'm reposting it. I have a button on my html page that takes the address someone puts in and deals with it to get back the lat long. In a C# file I have the code for the button, which is ending in getting the lat and long into Strings. How can I then display these strings on my html page?
Graham Carling
<asp:Button Text="Submit" runat="server" onclick="GetLatLong" ></asp:Button> This is the call in my html page for the method that gets the lat and longsaj
Post that as a new question, showing your markup and button click handler method.