Show Local Weather weird lat/long response results


I wonder if anyone can help:

In my show the local weather code the response from the API request to shows the weather for Shuzenji, Japan. This is a bit weird as my location is Sweden!

My code works like this:

In the $(document).ready(function() {
I check if the navigator.geolocation functionality is on and if so I call the function:
navigator.geolocation.getCurrentPosition(success, error);

In the success callback function I set the retrieved latitude and longitude values to 2 variables.
I use these variables to render a Google Map. This bit works great, and I get a map showing my correct location in Sweden- so everybody happy so far.
_ var img = new Image();_
_ img.src = googleMapsAPIURL + sLatitude + “,” + sLongitude + “&zoom=13&size=300x300&sensor=false”;_
_ output.appendChild(img);_

Here I also set a global var weather_url with the retrieved latitude and longitude values:
weather_url = “"+sLatitude+"&lon=”+sLongitude;

From within this success function I call another function that does the getJSON Get API request using the weather_url variable.

It is here that I get back an API response containing weather data for Shuzenji, Japan, and not my location in Sweden.

I have even checked the outgoing url from within the function that does the getJSON API call and can see that the supplied latitude and longitude variable values are correct for my location.

I am stumped as to what can be causing this. Any ideas/help would be fantastic as I have been stuck on this and trying different ways to solve this for a few days now.

Note, at the time of writing this post it seems the weather API is unreachable (I am getting a 504 gateway timeout response). Maybe it’s the API itself, but if anyone else has had similar issues it would be great to hear from you.

Thanks in advance

Do you made it on code pen? So we can check the entirely code. Or test it in our browser

I’ve had this problem once or twice before. I think it comes from the API.

I have just seen the problem!!!
It was an extra ampersand in the url I built!!!

weather_url = ""+sLatitude+"&lon="+sLongitude;
So the API must have been returning the data for Shuzenji, Japan as a default response.

Sorry for bothering you guys with this, and thanks for your help!

I will post up my code on CodePen and put a link to it here asap.
P.s. I wrote it on notepad and tested it on an online JavaScript editor

1 Like

Thanks for your reply - I thought it was the API but then when I made a direct call to the API in the document ready call - from an onclick event on the button. I wrote a test function that was called and the API was returning the correct data.

My URL variable I built in that test function was ok, whereas the URL variable I built in the function to first get the geolocation co-ordinates had an error in it - an extra ampersand.

Now it works fine.

Thanks for your help

1 Like

Well done for finding the bug. It was staring us in the face all along. Come to think of it, it’s been a while since I saw a response with Shuzenji. It’s very possible I had a bug in my code at that time, that has been fixed since. Now I wish I had used version control so I could check. Sorry for misleading you.

Hi All - I am also having this issue. I’ve tied the weather API pull to a button. Sometimes it returns my correct location, and sometimes it returns Shuzenji! Anyone else still having this issue?

Here is my codepen:

Just try clicking the button multiple times - sometimes it works and sometimes it doesn’t!

Hey, I have the same issue. I have a button as well to fetch the data from the API and after a few presses it starts to show the weather data of Shuzenji, Japan. Here is my pen under construction:

@HBI, @babokd - See the following discussion for more information.


Thank you for your response, Randell! Glad to know that it’s not isolated to my own implementation. It was driving me nuts!