Weather API concatenating coordinates

Weather API concatenating coordinates
0

#1

I’m working through this and I’ve found a way to get my device’s location with the IP address. What I’m not too sure of is how I’m supposed to take that response and then concatenate it into the request URL for the weather data. I keep getting an error that my variables in line 10 and 11 are not defined on line 19.

It seems like it’s just a scope issue that I just need more practice with. I’ve tried rearranging the functions, but currently I’m stumped.


#2

You need to move your call to the getWeather function to be inside the .get anonymous callback function (below):

$.get("https://ipinfo.io/json", function (response) {
    loc = response.loc;
    latlon = loc.split(',');
    lat = latlon[0];
    lon = latlon[1]; 
    getWeather();  
    });

You had it outside of the call to the ipinfo.io/json API and since the get function is asynchronous, the getWeather function was being called before the response was back from the API, so you were getting undefined for the lat and lon values.


#3

I knew it was something embarrassingly simple. Thanks :sweat_smile: