Introduction
Usually, reverse geocoding can be done in front end using JavaScript, whereas the below method can be used in web or Windows application to convert latitude and longitude into readable address.
Background
Google maps API provides variety of services in which I have used its reverse geocoding technique to get the XML data out of it and find the address of a particular place.
Using the Code
This method is simple and can be called from any function. You can integrate this method to your major code. It gets input as latitude and longitude and returns to you as an address.
public string ReverseGeoLoc(string longitude, string latitude,
out string Address_ShortName,
out string Address_country,
out string Address_administrative_area_level_1,
out string Address_administrative_area_level_2,
out string Address_administrative_area_level_3,
out string Address_colloquial_area,
out string Address_locality,
out string Address_sublocality,
out string Address_neighborhood)
{ Address_ShortName = "";
Address_country = "";
Address_administrative_area_level_1 = "";
Address_administrative_area_level_2 = "";
Address_administrative_area_level_3 = "";
Address_colloquial_area = "";
Address_locality = "";
Address_sublocality = "";
Address_neighborhood = ""; XmlDocument doc = new XmlDocument();
try
{ string place = String.Format
("http://maps.googleapis.com/maps/api/geocode/xml?
latlng=13.020164575569374,77.92610049247741&sensor=false"); doc.Load(place);
XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
if (element.InnerText == "ZERO_RESULTS")
{
return ("No data available for the specified location");
}
else
{ element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");
string longname="";
string shortname="";
string typename ="";
bool fHit=false;
XmlNodeList xnList = doc.SelectNodes
("//GeocodeResponse/result/address_component");
foreach (XmlNode xn in xnList)
{
try
{
longname = xn["long_name"].InnerText;
shortname = xn["short_name"].InnerText;
typename = xn["type"].InnerText;
fHit = true;
switch (typename)
{
case "country":
{
Address_country = longname;
Address_ShortName = shortname;
break;
} case "locality":
{
Address_locality = longname;
break;
} case "sublocality":
{
Address_sublocality = longname;
break;
}
case "neighborhood":
{
Address_neighborhood = longname;
break;
} case "colloquial_area":
{
Address_colloquial_area = longname;
break;
} case "administrative_area_level_1":
{
Address_administrative_area_level_1 = longname;
break;
} case "administrative_area_level_2":
{
Address_administrative_area_level_2 = longname;
break;
} case "administrative_area_level_3":
{
Address_administrative_area_level_3 = longname;
break;
}
default:
fHit = false;
break;
}
if (fHit)
{
Console.Write(typename);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("\tL: " + longname +
"\tS:" + shortname + "\r\n");
Console.ForegroundColor = ConsoleColor.Gray;
}
} catch (Exception e)
{
fHit = false;
Console.Write(" Invalid data: ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("\tX: " + xn.InnerXml + "\r\n");
Console.ForegroundColor = ConsoleColor.Gray;
}
}
return (element.InnerText);
} }
catch (Exception ex)
{
return ("(Address lookup failed: ) " + ex.Message);
}
}
Points of Interest
The code is reusable and you integrate it with any code for doing reverse geocoding from server side.