public async Task<List<ArticleModel>> GetArticlesByGpsCoordinateAsync(GeoLocation geolocation, float radius = 10000) { if (geolocation == null) { throw new ArgumentNullException(nameof(geolocation), @"Can not find coordinate"); } var articlesUrl = GetSearchArticlesUrl(geolocation, radius); var response = await _client.GetAsync(articlesUrl); // Will throw exception on unsuccessfull response. response.EnsureSuccessStatusCode(); var results = await response.Content.ReadAsAsync<WikiArticleResult>(); return results?.Query?.GeoSearchResults?.Select(article => new ArticleModel { Title = article.Title, PageId = article.PageId, Latitude = article.Latitude, Longitude = article.Longitude, Distance = article.Distance }).ToList() ?? new List<ArticleModel>(); }
/// <summary> /// Returns Url for given GeoLocation to get Json data /// </summary> /// <param name="geoLocation"></param> /// <param name="radius"></param> /// <param name="resultLimit"></param> /// <returns></returns> private static string GetSearchArticlesUrl(GeoLocation geoLocation, float radius = 10000, int resultLimit = 50) { if (radius < 10) radius = 10; if (radius > 100000) radius = 100000; if (resultLimit < 1) resultLimit = 1; if (resultLimit > 500) resultLimit = 500; return $"https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius={radius}&gscoord={geoLocation.Latitude}|{geoLocation.Longitude}&gslimit={resultLimit}&format=json"; }