I am trying to do the local weather project, but am unable to display the weather icon image. My html code is:
<img id="imageid" alt="Card image" target="_blank">
and my javascript code is:
var xcoord;
var ycoord;
var icon;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
xcoord = position.coords.longitude;
ycoord = position.coords.latitude;
console.log ("the x coord is: ", xcoord, " and the y coord is: ", ycoord);
$.getJSON("https://fcc-weather-api.glitch.me/api/current?lon=" + xcoord + "&lat=" + ycoord, function(json) {
$(".message").html(JSON.stringify(json));
var tempcel = json.main.temp;
var tempfah = Math.round(1.8 * tempcel + 32);
var windspeedkts = json.wind.speed;
var conditions = json.weather[0].description;
var city = json.name;
var country = json.sys.country;
icon = json.weather[0].icon;
console.log('the Temp is ', tempcel, "C or ", tempfah, "F");
console.log('the wind speed is ', windspeedkts, " knots");
console.log('the weather is ', conditions);
console.log('the location is ', city, " in ", country);
console.log("the weather icon is: ", icon);
document.getElementById("loc").innerHTML = "The current weather for " + json.name + " in " + json.sys.country + " is " + json.weather[0].description + ", the Temp is " + tempfah + " and the wind speed is " + json.wind.speed + " knots.";
document.getElementById("winds").innerHTML = "The current wind speed is " + json.wind.speed + " knots.";
document.getElementById("imageid").src = icon;
});
});
}