0

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;
                });
        });
    }
});
    

denov
  • 11,180
  • 2
  • 27
  • 43
Tyson Biegler
  • 68
  • 1
  • 11

1 Answers1

0

based on https://openweathermap.org/current#geo you're missing the https so you should have

const api = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${apiKey}`
denov
  • 11,180
  • 2
  • 27
  • 43