Problems with geolocation.getCurrentPosition

Problems with geolocation.getCurrentPosition
0

#1

I’m not exactly sure what’s going on, but I’m having problems with navigator.geolocation.getCurrentPosition for my weather application project. Any changes that are made in my success object are not altering variables, or permanently changing html. For example, the program is able to change the text of an element with the latitude, longitude but if I try to get the text of the element afterwards (even though the page has updated with the latitude/longitude), it returns an empty string. Additionally if I do something like change the value of a variable in the function, outside of the function nothing happens. What am I missing / how can I store the value of latitude / longitude for later use?

Here’s the codepen to what I’m trying to get working: https://codepen.io/Daniel_Lawson9999/pen/JOWeWW


#2

Seems to work fine. $("#geolocation").text() prints as expected and geo.test has a value (since it is defined in global scope).


#3

see your problem below:

var latitude = position.coords.latitude;
var longitude = position.coords.latitude;

#4

I think the errors are being misunderstood. This photo might help https://imgur.com/a/7Atdm . After getting the geolocation, the fields of the geo object remain undefined. I also created a variable called test with is initialized to 5, but the function changes it to 6. Everything that happens in the .getCurrentPosition function is not happening


#5

Actually it is working fine , the problem is you are trying to get the data before it has been fetched, since the function you are using works asynchronously, so, if you put this statement

console.log(geo.latitude + " " + geo.longitude);

inside your success function (after you run update) it will console.log your results. Oh, and you forgot to change your second location property to longitude , position.coords.longitude , right now you are getting two latitudes.