Skip to content

Instantly share code, notes, and snippets.

@egorguscha
Created November 7, 2018 14:57
Show Gist options
  • Save egorguscha/238f14d16d6b24e03874f5d3637d6b48 to your computer and use it in GitHub Desktop.
Save egorguscha/238f14d16d6b24e03874f5d3637d6b48 to your computer and use it in GitHub Desktop.
var weatherForecast = {
id: '',
country: '',
temperature: ''
}
var xhr = new XMLHttpRequest();
function query(url) {
xhr.open('GET', url);
xhr.addEventListener('readystatechange', function () {
if (xhr.readyState != xhr.DONE) { return; }
if (xhr.status != 200) {
console.log('Failed to get data from server. Check your request and try again')
} else {
var response = JSON.parse(xhr.responseText);
weatherForecast.id = response.id;
weatherForecast.country = response.name;
weatherForecast.temperature = response.main.temp;
document.querySelector('.title').innerText = weatherForecast.country;
document.querySelector('.item-value').innerText = `${weatherForecast.temperature} °C`;
}
})
xhr.send();
}
if ("geolocation" in navigator) {
// query('https://api.openweathermap.org/data/2.5/weather?id=630336&units=metric&APPID=1713de24b31c3508f06044a0598fa751');
navigator.geolocation.getCurrentPosition(function (pos) {
query(`https://api.openweathermap.org/data/2.5/weather?lat=${pos.coords.latitude}&long=${pos.coords.longitude}&units=metric&APPID=1713de24b31c3508f06044a0598fa751`)
console.log('lat', pos.coords.latitude);
console.log('long', pos.coords.longitude)
}, function (error) {
console.log(`${error.message}`)
})
} else {
console.log('Allow to use you location data')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment