Esempio n. 1
0
        public static OpenWeatherMapResult GetWeatherInformationForLocation(double latitude, double longitude)
        {
            var    geolocation = ReverseGeoLookup.ReverseGeoLoc(latitude, longitude);
            String response    = GetHelper(DECODERURLStart + geolocation.results[0].formatted_address + DECODERURLEnd);

            OpenWeatherMapResult result = JSONHelper.Deserialize <OpenWeatherMapResult>(response);

            return(result);
        }
Esempio n. 2
0
        public async Task <WeatherInfo> GetWeatherInfo()
        {
            WeatherInfo result = new WeatherInfo();

            if (string.IsNullOrEmpty(_location))
            {
                // Try to figure out location using GeoCoordinateWatcher with timeout
                InitiateGeoWatcher();
                await Task.Delay(TimeSpan.FromMilliseconds(_geoWatcherTimeout));

                if (string.IsNullOrEmpty(_location))
                {
                    throw new InsufficientDataException("Unable to retrieve location information");
                }
            }

            var locationQuery = LocationToQuery(_location);
            var unitQuery     = (_unit == TempUnit.K) ? "" : string.Format("&units={0}", unitParam[(int)_unit]);
            var query         = openweathermapUrl + _apiKey + '&' + locationQuery + unitQuery;

            using (var client = new HttpClient())
            {
                var res = await client.GetAsync(query, HttpCompletionOption.ResponseHeadersRead);

                if (res.IsSuccessStatusCode)
                {
                    var resAsByteArr = await res.Content.ReadAsByteArrayAsync();

                    MemoryStream ms         = new MemoryStream(resAsByteArr);
                    var          resWrapper = new OpenWeatherMapResult();
                    DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(OpenWeatherMapResult));
                    resWrapper      = (OpenWeatherMapResult)deserializer.ReadObject(ms);
                    result.Location = string.Format("{0} ({1})", resWrapper.name, resWrapper.sys.country);
                    result.Status   = string.Format(statusIconUrl, resWrapper.weather?[0].icon);
                    result.Temp     = resWrapper.main.temp;
                    result.TempMin  = resWrapper.main.temp_min;
                    result.TempMax  = resWrapper.main.temp_max;
                    result.Wind     = resWrapper.wind.speed;
                    result.Humidity = resWrapper.main.humidity;
                }
                else
                {
                    if (res.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new ArgumentException("Api Key is incorrect");
                    }
                    else if (res.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new ArgumentException("Provided location is not found");
                    }
                }
            }
            return(result);
        }