I just finished the “Show the local weather project”. The basic requirement is to show the current local weather using HTML5 geolocation or as some others have done; use an API for it. I’ve done the latter, using the ‘ipinfo.io/json’ API.
I’m having a problem though with displaying the correct location and weather. The problem I think is with a variable named userRegion. I’ve tried to set userRegion as a global variable but it turns up “undefined”, evident from console.log(userRegion). Could someone please advice?
Here’s the link to my project. https://codepen.io/surajr1711/pen/ZaGeoX/
I’ve tried to make it convenient for your review by commenting the jQuery with the following steps in respective areas.
//1. calling setup function to get geolocation.
//2. setup function gets geolocation using ipinfo API.
//3. API sends JSON data to gotData function.
//4. gotData function stores JSON location in userRegion global variable.
//5. PROBLEM! Why is userRegion “undefined”?
//6. calling getWeather function which uses openweathermap API.
//7. getWeather function calls openweathermap API using userRegion global variable as the query but userRegion is "undefined.
Please have a look guys. Let me know what I’m doing wrong. What am I missing?