I'm currently in the process of creating a weather app which will have two distinct API calls. One for latitude and longitude if the user allows the web app to access location and one where you can search weather by City. However, I am unsure why I keep getting an uncaught reference error telling me that latitude is not defined? As latitude and longitude are both set to equal their respective position.coords in my code. The best I can come up with is maybe a scope issue because of where they are defined, but I need them to be dynamic within the URL string??
Current JavaScript --
let LocationInfo = document.querySelector( ".location-weather" );
const cityInput = document.getElementById( "Search-Bubble" );
const searchButton = document.getElementById( "SearchBtn" );
window.addEventListener( "load", () => {
let latitude;
let longitude;
if ( navigator.geolocation ) {
navigator.geolocation.getCurrentPosition( ( position ) => {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
})
}
});
const apiCoordsUrl = `https://api.openweathermap.org/data/2.5/onecall?lat=${latitude}&lon=${longitude}&exclude=minutely,alerts&units=imperial&appid=${apiTag}`;
async function getCoordsWeatherInfo() {
const response = await fetch( apiCoordsUrl );
const data = await response.json();
this.unveilWeather( data );
};
console.log( getCoordsWeatherInfo() );