// http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding
 public static string GetReverseGeoLoc(Location location)
 {
     lock (locationNames)
     {
         string key = location.getID();
         if (locationNames.ContainsKey(key))
             return locationNames[key];
         return GetReverseGeoLocationNameFromMapService(location);
     }
 }
 public static Pair<string, string> GetReverseGeoLocAddress(Location location)
 {
     lock (locationAddresses)
     {
         string key = location.getID();
         if (locationAddresses.ContainsKey(key))
             return locationAddresses[key];
         return GetReverseGeoAddressFromMapService(location);
     }
 }
        private static string GetReverseGeoLocationNameFromMapService(Location location)
        {
            //return "Google -- Over query limit";

            XmlDocument doc = new XmlDocument();
            {
                doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + location.Lat + "," + location.Lng + "&sensor=false");
                XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
                /*if (element.InnerText == "OVER_QUERY_LIMIT")
                {

                    System.Threading.Thread.Sleep(new TimeSpan(0, 1, 10));
                    doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + location.Lat + "," + location.Lng + "&sensor=false");
                    element = doc.SelectSingleNode("//GeocodeResponse/status");

                }*/
                if (element.InnerText == "ZERO_RESULTS" || element.InnerText == "OVER_QUERY_LIMIT")
                    return "Google -- Over query limit";
                else
                {

                    element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");
                    locationNames.Add(location.getID(), element.InnerText);
                    WriteGeoLocationNames();
                    return element.InnerText;
                }
            }
        }
        private static Pair<string, string> GetReverseGeoAddressFromMapService(Location location)
        {
            Pair<string, string> address = new Pair<string, string>();
            XmlDocument doc = new XmlDocument();
            doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + location.Lat + "," + location.Lng + "&sensor=false");
            XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
            /*if (element.InnerText == "OVER_QUERY_LIMIT")
            {
                System.Threading.Thread.Sleep(new TimeSpan(0, 1, 10));
                doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + location.Lat + "," + location.Lng + "&sensor=false");
                element = doc.SelectSingleNode("//GeocodeResponse/status");

            }*/
            if (!(element.InnerText == "ZERO_RESULTS" || element.InnerText == "OVER_QUERY_LIMIT"))
            {
                var streetNumberNode =
                    doc.SelectSingleNode(
                        "//GeocodeResponse/result/address_component[type=\"street_number\"]/short_name");
                string street_number = streetNumberNode != null ? streetNumberNode.InnerText : "";

                var routeNode =
                    doc.SelectSingleNode("//GeocodeResponse/result/address_component[type=\"route\"]/short_name");
                string route = routeNode != null ? routeNode.InnerText : "";

                var postalCodeNode =
                    doc.SelectSingleNode(
                        "//GeocodeResponse/result/address_component[type=\"postal_code\"]/short_name");
                string postal_code = postalCodeNode != null ? postalCodeNode.InnerText : "";

                address = new Pair<string, string>(street_number + " " + route, postal_code);
                locationAddresses.Add(location.getID(), address);
                WriteGeoLocationAddresses();
                return address;

            }
            else
            {
                return new Pair<string, string>("reached query limit", "reached query limit");
            }
        }