Yet Another Cycling Forum

General Category => The Knowledge => Ctrl-Alt-Del => Topic started by: Diver300 on 25 July, 2019, 11:58:04 am

Title: Javascript location of device problem
Post by: Diver300 on 25 July, 2019, 11:58:04 am
I having trouble getting javascript to find where a webpage is being viewed from.

Strangely, I can run a javascript function that can find out the position, but I don't seem to be able to use it outside the function. Also the javascript function does not seem to complete, and code that follows the function call is run before most of the function.

Anyhow, here is the function on it's own, along with the source:- https://mtrak.co.uk/javalocation.php (https://mtrak.co.uk/javalocation.php)

The code says that the lat and long are undefined, in code that runs after those should have been found.

Any suggestions appreciated.
Title: Re: Javascript location of device problem
Post by: Ben T on 25 July, 2019, 12:08:40 pm
It doesn't run after they've been found though. It runs after you've requested that it go and find them. It's asynchronous.
Anything that relies on the values, you need to trigger from the showPosition2 function.
Title: Re: Javascript location of device problem
Post by: grams on 25 July, 2019, 12:12:18 pm
"showPosition2" will be called at a later time when the location has been determined.

The last two lines of your sample will be run immediately, when the location most definitely won't have been defined.

Basically the code that needs to use your location needs to be called from showPosition2.
Title: Re: Javascript location of device problem
Post by: Diver300 on 25 July, 2019, 12:23:35 pm
Thanks for that.

Although it will mean a significant rearrangement of the code, it does seem that I'll put what needs the result within the ShowPostion function.