Issues with the Weather API

Issues with the Weather API
0.0 0

#1

Hey guys. So I’m nearly done making the weather APP project, but I’m having a slight issue. My code seems to be only working half the time. Sometimes it shows me the correct location, and other times it shows the fallback in Japan/Shuzenji, but I am not entirely sure why. Even when I look at the URL, I see the correct lat/long of my location IRL, but it still shows Japan sometimes. Anyone know why this might be happening?

My code:

var lat;
var long;

function getLocation() {
navigator.geolocation.getCurrentPosition(showPosition);
}

getLocation();

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;

//}

var xmlhttp = new XMLHttpRequest(),
method = ‘GET’,
url = ‘https://fcc-weather-api.glitch.me/api/current?lat=’ + lat + ‘&lon=’ + long;
console.log(url);

xmlhttp.open(method, url, true);
xmlhttp.onload = function test() {
var response = JSON.parse(this.responseText);
var weather = response.weather[0].main;
var temperature = response.main.temp;
var country = response.sys.country;
var city = response.name;
console.log(weather);
console.log(temperature);
console.log(city);
console.log(country);
};

xmlhttp.send();

}


#2

See the following thread.