Ejemplo n.º 1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Dodavanje lokacije
            Lokacija l = new Lokacija();

            l.Naziv    = this.TrenutnaLokacijaUnos.Text;
            l.Omiljena = false;
            if (l.Naziv == "Trenutna Lokacija")
            {
                Lokacije.Add(l);
                WriteLokacije();
            }
            else
            {
                try
                {
                    using (WebClient webClient = new WebClient())
                    {
                        string url    = "http://api.openweathermap.org/data/2.5/weather?q=" + l.Naziv + "&units=metric&APPID=8e17202912490c577a70504fd76979f3";
                        string json   = webClient.DownloadString(url);
                        var    result = JsonConvert.DeserializeObject <WeatherInfo.root>(json);

                        WeatherInfo.root output = result;
                        if (output.cod != 200)
                        {
                            MessageBox.Show("Uneli ste nepostojecu lokaciju");
                        }
                        else
                        {
                            Lokacije.Add(l);
                            WriteLokacije();
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Uneli ste nepostojecu lokaciju");
                }
            }
        }
Ejemplo n.º 2
0
        public void LoadCurrent()
        {
            using (WebClient webClient = new WebClient())
            {
                string url;
                if (ipLokacija)
                {
                    string ipJson   = webClient.DownloadString("http://ip-api.com/json/");
                    var    ipResult = JsonConvert.DeserializeObject <IPLoc>(ipJson);
                    url = "http://api.openweathermap.org/data/2.5/weather?lat=" + ipResult.lat + "&lon=" + ipResult.lon + "&units=metric&APPID=8e17202912490c577a70504fd76979f3";
                }
                else
                {
                    url = "http://api.openweathermap.org/data/2.5/weather?q=" + TrenutnaLokacija + "&units=metric&APPID=8e17202912490c577a70504fd76979f3";
                }
                string json   = webClient.DownloadString(url);
                var    result = JsonConvert.DeserializeObject <WeatherInfo.root>(json);

                WeatherInfo.root output = result;

                // var cTemp = output.main.temp;
                var min = output.main.temp_min;
                var max = output.main.temp_max;
                result.weather[0].icon = "http://openweathermap.org/img/w/" + result.weather[0].icon + ".png";


                string temp     = output.main.temp.Split('.')[0] + "˚C";
                string min_temp = output.main.temp_min.Split('.')[0] + "˚C";;
                string max_temp = output.main.temp_max.Split('.')[0] + "˚C";;

                Humidity = output.main.humidity + "%";
                Wind     = output.wind.speed + "m/s";
                Clouds   = output.clouds.all + "%";
                Temp     = temp;
                MinTemp  = min_temp;
                MaxTemp  = max_temp;
                Icon_    = result.weather[0].icon;
            }
            LastUpdateString = DateTime.Now.ToString("dd/MM/yyyy H:mm");
        }