Exemplo n.º 1
0
        public static async Task <WeatherData> GetWeather(double latitude, double longitude)
        {
            var weatherData = new WeatherData();

            if (Reachability.IsHostReachable(DarkSky_API.WebApi()))
            {
                string queryString = DarkSky_API.AuthenticatedBaseURL()
                                     + latitude.ToString() + "," + longitude.ToString();

                var response = await GetDataFromService(queryString).ConfigureAwait(false);

                weatherData.Time = Conversions.ConvertFromUnixTimeToLocalTime(response.currently.time);
                TimeZoneInfo timezone;
                try
                {
                    timezone = TimeZoneInfo.FindSystemTimeZoneById(response.timezone);
                    weatherData.TimezoneTime    = Conversions.ConvertFromUnixTimeToTimeZoneTime(response.currently.time, timezone);
                    weatherData.TimezoneIsValid = true;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception {0}", ex);
                    Debug.WriteLine(String.Format("Failed to find timezone {0}.", response.timezone));
                    weatherData.TimezoneIsValid = false;
                }

                weatherData.Summary     = response.currently.summary;
                weatherData.Latitude    = response.latitude;
                weatherData.Longitude   = response.longitude;
                weatherData.WindSpeed   = response.currently.windSpeed;
                weatherData.Temperature = response.currently.temperature;
                weatherData.Humidity    = (int)(response.currently.humidity * 100.0);
                weatherData.Icon        = response.currently.icon;
                weatherData.TimeZone    = response.timezone;
                weatherData.UnixTime    = response.currently.time;

                var dailyWeatherForecast = response.daily.data;
                var weatherDayDataList   = new List <WeatherDayData>();
                foreach (var item in dailyWeatherForecast)
                {
                    WeatherDayData weatherDayData = new WeatherDayData();
                    weatherDayData.Time           = Conversions.ConvertFromUnixTimeToLocalTime(item.time);
                    weatherDayData.Icon           = item.icon;
                    weatherDayData.WindSpeed      = item.windSpeed;
                    weatherDayData.TemperatureMin = item.temperatureMin;
                    weatherDayData.TemperatureMax = item.temperatureMax;
                    weatherDayData.Humidity       = (int)(item.humidity * 100.0);
                    weatherDayDataList.Add(weatherDayData);
                }
                weatherData.DailyData = weatherDayDataList;
            }
            return(weatherData);
        }
Exemplo n.º 2
0
        private void AttemptToRequestLocation()
        {
            if (AppDelegate.Current.IsLocationServicesEnabled())
            {
                if (!AppDelegate.Current.IsLocationAuthorized())
                {
                    // request permission - either always or when in use
                    AppDelegate.Current.RequestWhenInUseAuthorization();
                }
                else
                {
                    //this is only requires, if constantly updating location
                    //AppDelegate.Current.StartCoreLocationUpdates();

                    if (Reachability.IsHostReachable(DarkSky_API.WebApi()))
                    {
                        //one-off request
                        AppDelegate.Current.RequestLocation();
                    }
                    else
                    {
                        //Display Alert
                        var okAlertController = UIAlertController.Create("Not connected to internet.",
                                                                         "Please try again later.", UIAlertControllerStyle.Alert);
                        okAlertController.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                        PresentViewController(okAlertController, true, null);
                    }
                }
            }
            else
            {
                //Display Alert
                var okAlertController = UIAlertController.Create("Please enable",
                                                                 "Location Services", UIAlertControllerStyle.Alert);
                okAlertController.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                PresentViewController(okAlertController, true, null);
            }
        }