public GelocationCacheItem PerformLocationLookup(double latitude, double longitude, List <Country> ocmCountryList) { //http://api.geonames.org/countryCodeJSON?formatted=true&lat=41.55111&lng=10.2&username<userid>&style=full //http://api.geonames.org/findNearbyPlaceNameJSON?formatted=true&lat=47.3&lng=9&username=<userid>&style=full try { //round position to 3 decimal places, or 111 meters accuracy latitude = Math.Round(latitude, 3); longitude = Math.Round(longitude, 3); //lookup item in existing cache, if not present create new item var cacheHit = GeolocationCache.FirstOrDefault(g => g.Latitude == latitude && g.Longitude == longitude); if (cacheHit != null) { return(cacheHit); } else { GelocationCacheItem cacheItem = new GelocationCacheItem(); string serviceURL = CountryCodeLookupServiceURL.Replace("{latitude}", latitude.ToString()).Replace("{longitude}", longitude.ToString()).Replace("{userid}", GeonamesAPIUserName); WebRequest request = WebRequest.Create(serviceURL); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) { string responseText = streamReader.ReadToEnd(); if (responseText.Contains("countryName")) { //example response /*{ * "languages": "de-AT,hr,hu,sl", * "distance": 0, * "countryName": "Austria", * "countryCode": "AT" * }*/ JObject responseObject = JObject.Parse(responseText); cacheItem.CountryName = responseObject["countryName"].ToString(); cacheItem.CountryCode = responseObject["countryCode"].ToString(); cacheItem.LanguageCodes = responseObject["languages"].ToString(); cacheItem.Longitude = longitude; cacheItem.Latitude = latitude; var countryInfo = ocmCountryList.FirstOrDefault(o => o.ISOCode == cacheItem.CountryCode || o.Title == cacheItem.CountryName); if (countryInfo != null) { cacheItem.CountryID = countryInfo.ID; } else { LogHelper.Log("Country not found in OCM list:" + cacheItem.CountryName); } LogHelper.Log("Geocache item created:" + cacheItem.CountryName); GeolocationCache.Add(cacheItem); SaveCache(); } } return(cacheItem); } } catch (Exception) { //failed return(null); } }
public GelocationCacheItem PerformLocationLookup(double latitude, double longitude, List<Country> ocmCountryList) { //http://api.geonames.org/countryCodeJSON?formatted=true&lat=41.55111&lng=10.2&username<userid>&style=full //http://api.geonames.org/findNearbyPlaceNameJSON?formatted=true&lat=47.3&lng=9&username=<userid>&style=full try { //round position to 3 decimal places, or 111 meters accuracy latitude = Math.Round(latitude, 3); longitude = Math.Round(longitude, 3); //lookup item in existing cache, if not present create new item var cacheHit = GeolocationCache.FirstOrDefault(g => g.Latitude == latitude && g.Longitude == longitude); if (cacheHit != null) { return cacheHit; } else { GelocationCacheItem cacheItem = new GelocationCacheItem(); string serviceURL = CountryCodeLookupServiceURL.Replace("{latitude}", latitude.ToString()).Replace("{longitude}", longitude.ToString()).Replace("{userid}", GeonamesAPIUserName); WebRequest request = WebRequest.Create(serviceURL); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) { string responseText = streamReader.ReadToEnd(); if (responseText.Contains("countryName")) { //example response /*{ "languages": "de-AT,hr,hu,sl", "distance": 0, "countryName": "Austria", "countryCode": "AT" }*/ JObject responseObject = JObject.Parse(responseText); cacheItem.CountryName = responseObject["countryName"].ToString(); cacheItem.CountryCode = responseObject["countryCode"].ToString(); cacheItem.LanguageCodes = responseObject["languages"].ToString(); cacheItem.Longitude = longitude; cacheItem.Latitude = latitude; var countryInfo = ocmCountryList.FirstOrDefault(o => o.ISOCode == cacheItem.CountryCode || o.Title == cacheItem.CountryName); if (countryInfo != null) { cacheItem.CountryID = countryInfo.ID; } else { LogHelper.Log("Country not found in OCM list:" + cacheItem.CountryName); } LogHelper.Log("Geocache item created:" + cacheItem.CountryName); GeolocationCache.Add(cacheItem); SaveCache(); } } return cacheItem; } } catch (Exception) { //failed return null; } }