Weather Page: Getting wrong Latitude and Longitude

Weather Page: Getting wrong Latitude and Longitude
0

#1

Hi All

My code seems to be not getting the Lat and Long properly. I am in south east queensland (Australia) but the location name is showing as Shuzenji and the Latitude and Longitude never seems to get populated. I have included console log statements to show data in the browser console and the Lat and Long never get written to the console.

Codepen Editor View
Codepen Full Page View

Can anyone point me to where I need to look to remedy this issue.

Am I using $(document).ready() correctly ??
Should I create the functions outside of the $(document).ready() and only call then inside it.??
Should $(document).ready() be at the top or bottom of the code ??

TIA

Pete


#2

Something like this https://pastebin.com/FZijTYw5.

The problem is that you are sending a request to a preformatted URL that is not updated since its first initialization.

navigator.geolocation.getCurrentPosition needs some time to request information from the user, then it fires success and error callbacks.

So you need to make the $.getJSON request only after success callback is made. In your case it is fired immediately, with undefined latitude and longitude.

Also in jQuery 3

$(document).ready(function() {
  // Handler for .ready() called.
});

can be replaced with

$(function() {
  // Handler for .ready() called.
});

According to http://api.jquery.com/ready/


#3

Thanks @Implaier. I will try and work through this to understand it.

Thanks for your help


#4

Additional info on making ajax requests
https://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success