-
-
Save egorguscha/238f14d16d6b24e03874f5d3637d6b48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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