示例#1
0
        public override async Task <string> UpdateLocationQuery(LocationData location)
        {
            string query = string.Empty;
            var    coord = new WeatherUtils.Coordinate(location.latitude, location.longitude);
            var    qview = await GetLocation(coord);

            if (String.IsNullOrEmpty(qview.LocationQuery))
            {
                query = string.Format("lat={0}&lon={1}", coord.Latitude, coord.Longitude);
            }
            else
            {
                query = qview.LocationQuery;
            }

            return(query);
        }
示例#2
0
        public override async Task <LocationQueryViewModel> GetLocation(WeatherUtils.Coordinate coord)
        {
            LocationQueryViewModel location = null;

            string queryAPI = "https://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query=";
            string options  = "";
            string query    = string.Format("{0},{1}", coord.Latitude, coord.Longitude);
            Uri    queryURL = new Uri(queryAPI + query + options);

            OpenWeather.location result;
            WeatherException     wEx = null;

            try
            {
                // Connect to webstream
                HttpClient          webClient = new HttpClient();
                HttpResponseMessage response  = await webClient.GetAsync(queryURL);

                response.EnsureSuccessStatusCode();
                Stream contentStream = null;
#if WINDOWS_UWP
                contentStream = WindowsRuntimeStreamExtensions.AsStreamForRead(await response.Content.ReadAsInputStreamAsync());
#elif __ANDROID__
                contentStream = await response.Content.ReadAsStreamAsync();
#endif

                // End Stream
                webClient.Dispose();

                // Load data
                XmlSerializer deserializer = new XmlSerializer(typeof(OpenWeather.location));
                result = (OpenWeather.location)deserializer.Deserialize(contentStream);

                // End Stream
                if (contentStream != null)
                {
                    contentStream.Dispose();
                }
            }
            catch (Exception ex)
            {
                result = null;
#if WINDOWS_UWP
                if (WebError.GetStatus(ex.HResult) > WebErrorStatus.Unknown)
                {
                    wEx = new WeatherException(WeatherUtils.ErrorStatus.NetworkError);
                    await Toast.ShowToastAsync(wEx.Message, ToastDuration.Short);
                }
#elif __ANDROID__
                if (ex is WebException || ex is HttpRequestException)
                {
                    wEx = new WeatherException(WeatherUtils.ErrorStatus.NetworkError);
                    new Android.OS.Handler(Android.OS.Looper.MainLooper).Post(() =>
                    {
                        Toast.MakeText(Application.Context, wEx.Message, ToastLength.Short).Show();
                    });
                }
#endif
                Logger.WriteLine(LoggerLevel.Error, ex, "MetnoWeatherProvider: error getting location");
            }

            if (result != null && !String.IsNullOrWhiteSpace(result.query))
            {
                location = new LocationQueryViewModel(result);
            }
            else
            {
                location = new LocationQueryViewModel();
            }

            return(location);
        }
 // GeopositionQuery
 public abstract Task <LocationQueryViewModel> GetLocation(WeatherUtils.Coordinate coordinate);
 public async Task <LocationQueryViewModel> GetLocation(WeatherUtils.Coordinate coord)
 {
     return(await WeatherProvider.GetLocation(coord));
 }
示例#5
0
        public override async Task <LocationQueryViewModel> GetLocation(WeatherUtils.Coordinate coord)
        {
            LocationQueryViewModel location = null;

            string           yahooAPI       = "https://query.yahooapis.com/v1/public/yql?q=";
            string           location_query = string.Format("({0},{1})", coord.Latitude, coord.Longitude);
            string           query          = "select * from geo.places where text=\"" + location_query + "\"";
            Uri              queryURL       = new Uri(yahooAPI + query);
            place            result         = null;
            WeatherException wEx            = null;

            try
            {
                // Connect to webstream
                HttpClient          webClient = new HttpClient();
                HttpResponseMessage response  = await webClient.GetAsync(queryURL);

                response.EnsureSuccessStatusCode();
                Stream contentStream = null;
#if WINDOWS_UWP
                contentStream = WindowsRuntimeStreamExtensions.AsStreamForRead(await response.Content.ReadAsInputStreamAsync());
#elif __ANDROID__
                contentStream = await response.Content.ReadAsStreamAsync();
#endif

                // End Stream
                webClient.Dispose();

                // Load data
                XmlSerializer deserializer = new XmlSerializer(typeof(query));
                query         root         = (query)deserializer.Deserialize(contentStream);

                if (root.results != null)
                {
                    result = root.results[0];
                }

                // End Stream
                if (contentStream != null)
                {
                    contentStream.Dispose();
                }
            }
            catch (Exception ex)
            {
                result = null;
#if WINDOWS_UWP
                if (Windows.Web.WebError.GetStatus(ex.HResult) > Windows.Web.WebErrorStatus.Unknown)
                {
                    wEx = new WeatherException(WeatherUtils.ErrorStatus.NetworkError);
                    await Toast.ShowToastAsync(wEx.Message, ToastDuration.Short);
                }
#elif __ANDROID__
                if (ex is System.Net.WebException || ex is HttpRequestException)
                {
                    wEx = new WeatherException(WeatherUtils.ErrorStatus.NetworkError);
                    new Android.OS.Handler(Android.OS.Looper.MainLooper).Post(() =>
                    {
                        Toast.MakeText(Application.Context, wEx.Message, ToastLength.Short).Show();
                    });
                }
#endif
                Logger.WriteLine(LoggerLevel.Error, ex, "YahooWeatherProvider: error getting location");
            }

            if (result != null && !String.IsNullOrWhiteSpace(result.woeid))
            {
                location = new LocationQueryViewModel(result);
            }
            else
            {
                location = new LocationQueryViewModel();
            }

            return(location);
        }