public WeatherData GetWeatherData(Location location) { var url = string.Format("http://api.openweathermap.org/data/2.5/weather?q=" + location.GetLocation() + "&units=metric&mode=xml&APPID=" + ApiKey); var wc = new WebClient(); string reply = wc.DownloadString(url); WeatherData weather = new WeatherData(); XDocument ob; try { ob = XDocument.Parse(reply); } catch (Exception e) { throw new WeatherDataServiceException(e.Message + "\nerror: city not found"); } weather.cityName = ob.Root.Element("city").Attribute("name").Value; weather.country = ob.Root.Element("city").Element("country").Value; weather.id = ob.Root.Element("city").Attribute("id").Value; weather.lon = ob.Root.Element("city").Element("coord").Attribute("lon").Value; weather.lat = ob.Root.Element("city").Element("coord").Attribute("lat").Value; weather.temperature = ob.Root.Element("temperature").Attribute("value").Value; weather.maxTemp = ob.Root.Element("temperature").Attribute("max").Value; weather.minTemp = ob.Root.Element("temperature").Attribute("min").Value; weather.humidity = ob.Root.Element("humidity").Attribute("value").Value + "%"; weather.windSpeed = ob.Root.Element("wind").Element("speed").Attribute("value").Value; weather.windDirection = ob.Root.Element("wind").Element("direction").Attribute("name").Value; weather.cloud = ob.Root.Element("clouds").Attribute("name").Value; weather.sunrise = ob.Root.Element("city").Element("sun").Attribute("rise").Value.Substring(11); weather.sunset = ob.Root.Element("city").Element("sun").Attribute("set").Value.Substring(11); return(weather); }
/// <summary> /// Tries to use the users location do get a city list from weather api /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void btnUseLocation_Tapped(object sender, TappedRoutedEventArgs e) { //Hide error message tblLocationDisabledMessage.Visibility = Visibility.Collapsed; //Hide search error message tblNoSearchResult.Visibility = Visibility.Collapsed; //Hide unknown error message tblUnknownError.Visibility = Visibility.Collapsed; //Hide connection error image imgConnectionError.Visibility = Visibility.Collapsed; //Set the loading image to visible imgLoading.Visibility = Visibility.Visible; //Run in a different thread so no blocking await System.Threading.Tasks.Task.Run(() => { //Get the location Location.GetLocation( (pos) => { //Load a city from accu Weather this.WAC.FindCityByCoordinates(pos, (result) => { //Convert the result to City City city = new City { Version = result.Version, Key = result.Key, Type = result.Type, Rank = result.Rank, LocalizedName = result.LocalizedName, AdministrativeArea = new AdministrativeArea { ID = result.AdministrativeArea.ID, LocalizedName = result.AdministrativeArea.LocalizedName }, Country = new Country { ID = result.Country.ID, LocalizedName = result.Country.LocalizedName } }; //Do the callback in UI thread IAsyncAction a = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { //Add the city to the list this.cities = new List <City> { city }; //Show the list view lvCities.Visibility = Visibility.Visible; //Place the city to the search result list view lvCities.ItemsSource = this.cities; }); return(true); }, (err) => { //Do the callback in UI thread IAsyncAction a = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { //Hide the list view lvCities.Visibility = Visibility.Collapsed; //Show error message tblNoSearchResult.Visibility = Visibility.Visible; //Hide the loading image imgLoading.Visibility = Visibility.Collapsed; //Show connection error image imgConnectionError.Visibility = Visibility.Visible; }); return(true); }); return(true); }, (err) => { System.Diagnostics.Debug.WriteLine(err); //Check if it is a permission error or it is an unknonwn error if (err == "Access to location is denied.") { //Show error message tblLocationDisabledMessage.Visibility = Visibility.Visible; //Hide the list view lvCities.Visibility = Visibility.Collapsed; } else { //Show unknown error message tblUnknownError.Visibility = Visibility.Visible; //Show the list view lvCities.Visibility = Visibility.Visible; } //Hide the loading image imgLoading.Visibility = Visibility.Collapsed; return(true); } ); }); }