public async Task <List <GeocodeResult> > ExecuteQuery(string query) { query = Uri.EscapeUriString(query); string URL = String.Format( "http://open.mapquestapi.com/nominatim/v1/search?format=xml&q={0}&addressdetails=0&limit={1}&countrycodes=at&exclude_place_ids=613609", query, 15); // max. results to return, possibly make this configurable string response = ""; using (var client = new HttpClient()) { response = await client.GetStringAsync(URL); client.Dispose(); } var searchresults = XElement.Parse(response); var mapped = searchresults.Elements("place") .Select(e => new GeocodeResult() { Name = (string)e.Attribute("display_name"), Longitude = MappingHelpers.ConvertDouble((string)e.Attribute("lon")), Latitude = MappingHelpers.ConvertDouble((string)e.Attribute("lat")), }) .ToList(); return(mapped); }
// // Copy/Paste Sample: http://open.mapquestapi.com/nominatim/v1/reverse?format=xml&lat=51.521435&lon=-0.162714 // public async Task <GeocodeResult> ReverseGeocode(double latitude, double longitude) { string URL = String.Format( "http://open.mapquestapi.com/nominatim/v1/reverse?format=xml&lat={0}&lon={1}", latitude.ToString(CultureInfo.InvariantCulture), longitude.ToString(CultureInfo.InvariantCulture)); string response = ""; using (var client = new HttpClient()) { response = await client.GetStringAsync(URL); client.Dispose(); } var searchresults = XElement.Parse(response); var resElement = searchresults.Elements("result").FirstOrDefault(); var addressElement = searchresults.Elements("addressparts").FirstOrDefault(); if (resElement != null && addressElement != null) { var countryCode = (string)addressElement.Element("country_code"); if (0 == String.Compare("at", countryCode, StringComparison.OrdinalIgnoreCase)) { // Only if City or Town is not available, we will fall back to the actual location name string locationName = (string)addressElement.Element("city"); if (String.IsNullOrWhiteSpace(locationName)) { locationName = (string)addressElement.Element("town"); if (String.IsNullOrWhiteSpace(locationName)) { locationName = (string)resElement; } } var result = new GeocodeResult() { Name = locationName, Latitude = MappingHelpers.ConvertDouble((string)resElement.Attribute("lat")), Longitude = MappingHelpers.ConvertDouble((string)resElement.Attribute("lon")) }; return(result); } } return(null); }
public async Task <GasQueryDownloadResult> DownloadAsync(GasQuery parameter) { // Prepare POST data string postData = Uri.EscapeUriString(parameter.ToPostData()); var ascii = new UTF8Encoding(); byte[] bData = ascii.GetBytes(postData); var content = new ByteArrayContent(bData); content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); List <RootObject> result = null; try { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); client.DefaultRequestHeaders.Referrer = new Uri("http://spritpreisrechner.at/ts/map.jsp"); HttpResponseMessage response = await client.PostAsync(URL, content); string json = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <List <RootObject> >(json); } } catch (Exception ex) { return(new GasQueryDownloadResult() { Succeeded = false, ErrorMessage = ex.ToString(), Result = new GasQueryResult(parameter) }); } // Errors are reported in a rather weird way so let's be extra careful in retrieving those if (result.Count == 1) { var firstItem = result.First(); if (firstItem != null) { var errorItems = firstItem.errorItems; if (errorItems != null && errorItems.Count > 0 && errorItems.First() != null) { return(new GasQueryDownloadResult() { Succeeded = false, ErrorMessage = errorItems.First().msgText, Result = new GasQueryResult(parameter) }); } } } var foundTanken = new List <GasStationResult>(); foreach (var ro in result) { if (null == ro) { continue; } if (!String.IsNullOrWhiteSpace(ro.gasStationName)) { var tanke = new GasStationResult() { City = ro.city, PostalCode = ro.postalCode, Street = ro.address, Name = ro.gasStationName, Longitude = MappingHelpers.ConvertDouble(ro.longitude), Latitude = MappingHelpers.ConvertDouble(ro.latitude), }; if ((null != ro.spritPrice) && ro.spritPrice.Count > 0) { var preisInfo = ro.spritPrice.First(); if ((null != preisInfo) && !String.IsNullOrWhiteSpace(preisInfo.amount)) { tanke.Price = MappingHelpers.ConvertDouble(preisInfo.amount); foundTanken.Add(tanke); } } } } var orderedTanken = foundTanken.OrderBy(t => t.Price).ToList(); int tankeOrderNumber = 1; foreach (var tanke in orderedTanken) { tanke.UniqueId = tankeOrderNumber.ToString(); tankeOrderNumber++; } return(new GasQueryDownloadResult() { Succeeded = true, Result = new GasQueryResult(parameter, orderedTanken, DateTime.Now) }); }