Beispiel #1
0
        private async Task <GoogleMapApi> locationGoogleApi()
        {
            //get location lat and lon
            Geolocator  gl = new Geolocator();
            Geoposition gp = await gl.GetGeopositionAsync();

            string country   = gp.CivicAddress.Country;
            string latitude  = gp.Coordinate.Latitude.ToString();
            string longitude = gp.Coordinate.Longitude.ToString();
            //get object
            HttpClient client = new HttpClient();
            string     res    = await client.GetStringAsync("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&key=" + YOUR_API_KEY);

            GoogleMapApi googleapi = JsonConvert.DeserializeObject <GoogleMapApi>(res);

            return(googleapi);
        }
Beispiel #2
0
        private async Task <WeatherApi> parseJsonObj()
        {
            GoogleMapApi cityapi = await locationGoogleApi();

            string           city    = "";
            AddressComponent address = cityapi.results[0].address_components[3];

            city = address.short_name;
            //Short city
            Geolocator  gl = new Geolocator();
            Geoposition gp = await gl.GetGeopositionAsync();

            string country = gp.CivicAddress.Country;
            //VN
            HttpClient web      = new HttpClient();
            string     response = await web.GetStringAsync("http://api.openweathermap.org/data/2.5/weather?APPID=" + APPID + "&q=" + city + "," + country);

            WeatherApi rootobj = JsonConvert.DeserializeObject <WeatherApi>(response);

            return(rootobj);
        }
Beispiel #3
0
        private async void getCurrent()
        {
            WeatherApi rootobj = await parseJsonObj();


            //Address
            GoogleMapApi api = await locationGoogleApi();

            //string address = api.results[0].formatted_address;
            AddressComponent city    = api.results[0].address_components[3];
            AddressComponent country = api.results[0].address_components[4];
            string           address = city.long_name + ", " + country.long_name;

            tbAddress.Text = address;

            //Temperature
            Main main = rootobj.main;
            //string mintemp = "Thấp " + ((int)(main.temp_min - 273.1)).ToString() + "℃ ";
            //string maxtemp = "Cao " + ((int)(main.temp_max - 273.1)).ToString() + "℃";
            // string maintemp = ((int)(main.temp - 273.1)).ToString() + "°";
            string maintemp = Math.Round((main.temp - 273.1), 2).ToString() + "°";

            //tbtempMain.Text = mintemp + maintemp + maxtemp;
            tbtempMain.Text = maintemp;

            //Weather Descreption
            string wetherdes = rootobj.weather[0].description;

            string[] word = wetherdes.Split(' ');

            string wedes = null;

            for (int i = 0; i < word.Length; i++)
            {
                wedes += Regex.Replace(word[i], "^[a-z]", m => m.Value.ToUpper()) + " ";
            }
            tbWeatherDes.Text = wedes;
            //Feels Like
            tbFeelsLike.Text = "Feels Like " + maintemp;
            //Humidity
            tbHumidity.Text = "Humidity " + rootobj.main.humidity.ToString() + "%";
            //Atmospheric pressure
            tbAtmosPressure.Text = "Pressure " + Math.Round((rootobj.main.grnd_level * 0.000986923267), 2).ToString() + "atm";
            //Wind
            double windeg    = rootobj.wind.deg;
            string windegStr = (0 <= windeg && windeg < 22.5) ? "North-northeast" : (22.5 <= windeg && windeg < 45) ? "Northeast" :
                               (45 <= windeg && windeg < 67.5) ? "East-northeast" :
                               (67.5 <= windeg && windeg < 90) ? "East" : (90 <= windeg && windeg < 112.5) ? "East-southeast" :
                               (112.5 <= windeg && windeg < 135) ? "Southeast" :
                               (135 <= windeg && windeg < 157.5) ? "South-southeast" : (157.5 <= windeg && windeg < 180) ? "South" :
                               (180 <= windeg && windeg < 202.5) ? "South-southwest" :
                               (202.5 <= windeg && windeg < 225) ? "Southwest" : (225 <= windeg && windeg < 247.5) ? "West-southwest" :
                               (247.5 <= windeg && windeg < 270) ? "West" :
                               (270 <= windeg && windeg < 292.5) ? "West-northwest" : (292.5 <= windeg && windeg < 315) ? "Northwest" :
                               (315 <= windeg && windeg < 337.5) ? "North-northwest" :
                               "North";


            tbWind.Text = "Wind speed " + Math.Round((rootobj.wind.speed * 3.6), 2) + "km/h" + "\n" +
                          "          " + windegStr + " (" + rootobj.wind.deg + "°" + ")";
            //Clouds
            tbCloud.Text = "Cloudiness " + rootobj.clouds.all + "%";
            //Sunrise
            DateTime daterise = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

            daterise       = daterise.AddSeconds(rootobj.sys.sunrise).ToLocalTime();
            tbSunrise.Text = "Sunrise " + daterise.Hour + ":" + daterise.Minute;
            //Sunset
            DateTime dateset = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

            dateset       = dateset.AddSeconds(rootobj.sys.sunset).ToLocalTime();
            tbSunset.Text = "Sunset " + dateset.Hour + ":" + dateset.Minute;


            //Icon current weather
            string icon     = rootobj.weather[0].icon;
            string httpicon = "http://openweathermap.org/img/w/" + icon + ".png";

            iconWeatherCurrent.Source = new BitmapImage(new Uri(httpicon));
        }