I've been trying to make a website which shows the user's local weather. everything works fine, exept the fact that, if you want to swich units, the browser asks you again to share your location and I think that somebody could find that anoying. How can I fix that? Here's the code:
var la;
var lg;
var celsius=1;
var tempChange;
function getLocation() {
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(showPosition);
}
function showPosition(position) {
la = position.coords.latitude;
lg = position.coords.longitude;
var url="https://fcc-weather-api.glitch.me/api/current?lat=" + la + "&lon=" + lg;
fetch(url)
.then((resp) => resp.json())
.then(function(data) {
if(celsius===1)
document.getElementById("temper").innerHTML="Temperature:" + data.main.temp + " °C";
else
document.getElementById("temper").innerHTML="Temperature:" + (data.main.temp * 1.8 + 32) + " °F";
document.getElementById("image").src=data.weather[0].icon;
})
.catch(function(error) {
console.log("error: " + error);
});
}
getLocation();
function change() {
if(celsius===0)
celsius=1;
else
celsius=0;
getLocation();
}
Here's a jsfiddle with the full HTML and CSS code, but somehow the switch button doesn't work: https://jsfiddle.net/1pvjrw3n/ . Normally, the browser would ask again for your location, then change the displayed temperature. This happens everytime you want to switch units.