My question is about Javascript. I have a Callback function which receives a Position object on a successful callback.
The problem is that when I try to set the prop
You can't return a value from the callback (in this case). That would mean that inside of getCurrentPosition, the return value from the callback has to be assigned somewhere.
Assigning it to a global variable works, but at the time you access that variable, it was not assigned the new value yet. E.g.
// x is assigned in `onSuccess`
var x;
navigator.geolocation.getCurrentPosition(onSuccess, onError);
alert(x); // will be undefined, the response is not processed yet
Think about it: getCurrentPosition is probably doing an Ajax request to get the position. Such a request takes (a) time (couple of milliseconds) and because of that (b) is asynchronous, which means that JavaScript does not wait until the response is received. Your code is way faster. onSuccess was not called yet when you alert x.
Only solution:
All the code that has to access the position as to be in or called from the callback.