Need help in jQuery/ Ajax for Weather App

Need help in jQuery/ Ajax for Weather App
0.0 0

#1

I started building this project (Weather App) using FCC’s api. But there is something wrong with the variables storing coordinates. The variables are not updating , hence when I am doing getJson request I am not able to send the latitude longitude with the request url. Please see my code…
Please excuse me for too many console.log , I was trying to see what’s wrong.

$(document).ready(function () {
    var latlong=[];
    function getLocation() {
        if(navigator.geolocation){
            navigator.geolocation.getCurrentPosition(givePos);
        }


    }
    
    function givePos(pos) {
        var lat=pos.coords.latitude;
        var longt=pos.coords.longitude;
        console.log(lat);
        console.log(longt);
        latlong.push(lat,longt);
    }

    getLocation();
console.log(latlong);
    $.ajax({

        type:'GET',
        dataType: "json",
        url: 'https://fcc-weather-api.glitch.me/api/current?lon='+latlong[1]+'&lat='+latlong[0] ,

        success: function (response) {
            console.log(response);

        }
    });



});

I know I am messing something with Ajax or something to do with scope of variables.


#2
function givePos(pos) {
  var lat=pos.coords.latitude;
  var longt=pos.coords.longitude;
  latlong.push(lat,longt);
  $.ajax({
    type:'GET',
    dataType: "json",
    url: 'https://fcc-weather-api.glitch.me/api/current?lon='+latlong[1]+'&lat='+latlong[0],
    success: function (response) {
      console.log(response);
    }
  });
}