FreeCodeCamp´s weather API location errors. Anyone else?

FreeCodeCamp´s weather API location errors. Anyone else?
0

#1

Hi

I have been working on the local weather challenge for a while using FreeCodeCamp’s weather API and found that rather often it does not find my location correctly.

Let me explain:

the codepen does work. but every now and then the API seems to decide I am in japan rather than the UK.

Has anyone else encountered this problem as well?

Also, I found that one specific parameter (the location… again!!) loads perfectly when using a button, but does not normally load when loading the pen itself. I am puzzled as why this is happening, since the button executes the same function as the pen when loading.

Here you have link to my pen.

https://codepen.io/Graventh/full/vpMJpm/

(Sorry for the ugly looking style… I have been focusing on the functionality so far)

Thank you for your help!!


#2

I am surprised it is not always showing Japan. You are not understanding the asynchronous nature of navigator.geolocation.getCurrentPosition. Because it is asynchronous, other code can continue executing before a response comes back with the latitude and longitude values that your showPosition function assigns to myLatitude and myLongitude.

So, with the following code:

getLocation();
getWeather();

While the waiting on a response to come back from the navigator.geolocation.getCurrentPosition API insdie the getLocation, the getWeather function is already running and since you assign the value 0 to both myLatitude and myLongitude at the beginning of your code, when you attempt to build call the fcc-weather-api using myLatitude and myLongitude, the API does not accept those 0 values as valid, so it defaults to the coordinates for Japan.

How do you solve this issue? Move your call to getWeather to the end of your showPosition function.


#3

OK, I think I get it now. I am used to coding that executes sequentially instead of simultaneously. I didn´t think about the possibility of the two functions executing at the same time!!

Thank you, for the explanation! Everything makes much more sense now.