Need Help with Query on Local Weather App

Hello everybody!!

I’m using openweathermap’s API to obtain local weather. as you can see on my codepen I’m getting lat and lon values to form the query to obtain the JSON file with all the info. However, I´m getting all this values inside the geoSuccess function, which make them unavailable from outside the function.

I know this may be a very basic mistake but I have no idea how to solve this, so any advice would be highly appreciated. :sweat_smile:

My Codepen

I think you need to declare the variables globally then only you will be able to access them outside the function, also if you try to use this variables before this function is called it will not work because until the variables are initialized they will output undefined so i suggest you call the function first thing as the page loads i.e call the navigator.geolocation.getCurrentPosition() right at the top, this will eventually call the functions inside the method and your global variables will be initialized to the value you intended them to and then you can use these variables right after it.Hope this helps!

It’s generally a bad practice to have global variables. Try using calling $.ajax within another function and then returning the results like so:

function myApi(lat, lng) {
var data;

$.ajax

.success(function(reply) {
data = reply;
}

return data;
}