Adding location data to openweather API

My problem is when I try to add the ${lat} and ${long} to the API string. openweathermap docs say to put in numbers for the latitude and longitude but I want to have them inserted from the variables above that are gotten from ‘position’

window.addEventListener('load', () => {
    let long;
    let lat;
    let temperatureDescription = document.querySelector('.temperature-description');
    let temperatureDegree = document.querySelector('.temperature-degree');
    let locationTimezone = document.querySelector('.location-timezone');

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(position => {
            long = position.coords.longitude;
            lat = position.coords.latitude;

            console.log(position)

            const api = `api.openweathermap.org/data/2.5/weather?lat=**${lat}&lon=${long}&appid=<API-CODE>`;

            fetch(api)
                .then(response => {
                    return response.json();
                })
                .then(data => {
                    console.log(data);
                    const { temperature, summary } = data.currently;
                    // Set DOM elements from the API
                    temperatureDegree.textContent = temperature;
                    temperatureDescription.textContent = summary;
                });
        });
    }
});
    

50 thoughts on “Adding location data to openweather API”