public async Task <GeoSearchResult> ReverseGeocode(decimal latitude, decimal longitude)
        {
            var geoSearchResult = new GeoSearchResult();

            using (var client = new HttpClient())
            {
                var    revGeoUri      = new Uri(Inv($"http://api.geonames.org/findNearbyJSON?lat={latitude}&lng={longitude}&fclass=P&fcode=PPLA&fcode=PPL&fcode=PPLC&username={WebUtility.UrlEncode(Config.Username)}"));
                string revGeoResponse = await client.GetStringAsync(revGeoUri);

                PopulateWithJsonString(geoSearchResult, revGeoResponse);
            }
            return(geoSearchResult);
        }
        public async Task <string> GetFirstReverseGeo(decimal latitude, decimal longitude)
        {
            GeoSearchResult result = await ReverseGeocode(latitude, longitude);

            if (result == null)
            {
                return(null);
            }

            return(result.GeoNames.Any()
                ? result.GeoNames[0].NameAndCountryName
                : null);
        }
        public async Task <GeoSearchResult> SearchForLocation(string location)
        {
            var geoSearchResult = new GeoSearchResult();

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.ConnectionClose = true;

                var geoSearchUri = new Uri(
                    $"http://api.geonames.org/searchJSON?maxRows=1&q={WebUtility.UrlEncode(location)}&username={WebUtility.UrlEncode(Config.Username)}"
                    );
                string geoSearchResponse = await client.GetStringAsync(geoSearchUri);

                PopulateWithJsonString(geoSearchResult, geoSearchResponse);
            }
            return(geoSearchResult);
        }
        public async Task <GeoName> GetFirstGeoName(string query)
        {
            PostCodeSearchResult postCodes = await SearchForPostCode(query);

            if (postCodes != null)
            {
                return(postCodes.PostCodeEntries.Any()
                    ? postCodes.PostCodeEntries[0]
                    : null);
            }

            GeoSearchResult result = await SearchForLocation(query);

            if (result == null)
            {
                return(null);
            }

            return(result.GeoNames.Any()
                ? result.GeoNames[0]
                : null);
        }