private static string ReverseGeocode(double lat, double lon)
{
string res = null;
try {
if (Reachability.IsHostReachable ("www.google.com")) {
WebClient client = new WebClient ();
string output = client.DownloadString (string.Format ("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=true", lat, lon));
XElement root = XElement.Parse (output);
var items = from item in root.Descendants("result")
select new
{
AddressType = item.Element("type").Value,
FormattedAddress = item.Element("formatted_address").Value
};
foreach (var item in items) {
string[] parts = item.FormattedAddress.Split (',');
if (parts.Length >= 2) {
res = string.Format ("{0}, {1}", parts [0].Trim (), parts [1].Trim ());
return res;
} else {
res = item.FormattedAddress;
return res;
}
}
}
return "Unknown";
} catch (Exception ex) {
Util.Log ("Exception in reverse geocode: {0}", ex.Message);
return "Unknown";
}
return "Unknown";
}