- Back to Home »
- ASP.Net , C# »
Posted by : Jebastin
Thursday, 19 December 2013
The following C# Code is used to get the Latitude and Longitude Co-Ordinates of a particular place anywhere in the World using the Address or Zip-code. To get the Latitude and Longitude Co-Ordinates using Address, call the function like this, getLatLong("Briarwood, NY 11435-2694",""). To get the Latitude and Longitude Co-Ordinates using the Zip-code, call the function like this, getLatLong("","11435").
- public string getLatLong(string Address, string Zip)
- {
- string latlong = "", address = "";
- if (Address!=string.Empty)
- {
- address = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + Address + "&sensor=false";
- }
- else
- {
- address = "http://maps.googleapis.com/maps/api/geocode/xml?components=postal_code:" + Zip.Trim() + "&sensor=false";
- }
- var result = new System.Net.WebClient().DownloadString(address);
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(result);
- XmlNodeList parentNode = doc.GetElementsByTagName("location");
- var lat = "";
- var lng = "";
- foreach (XmlNode childrenNode in parentNode)
- {
- lat = childrenNode.SelectSingleNode("lat").InnerText;
- lng = childrenNode.SelectSingleNode("lng").InnerText;
- }
- latlong = Convert.ToString(lat) + "," + Convert.ToString(lng);
- return latlong;
- }
very good, thanks
ReplyDeleteWelcome!
Deletehello, is this paid service or free. it worked for a while then stopped. how can i use it again ?? please let me know. thanks
ReplyDeleteHello Sourab,
DeleteIt's a free service only but with some limitations.
Usage Limits for the users of the free API:
2,500 requests per 24 hour period.
5 requests per second.
Reference: https://developers.google.com/maps/documentation/geocoding/