I'm hoping someone with a lot more experience in JavaScript can help me because I think I am falling foul of asynchronous functions, variable scope and trying to figure out what to do. I have been through a number of answers on here but nothing helped me.
I am trying to get coordinates from the browser. This is code I found online which uses the browser:
<script>
'use strict';
var pos = '';
//if there is no geo, use browser html 5
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geopos = {
lng: position.coords.longitude,
lat: position.coords.latitude
}
console.log(geopos);
pos = geopos;
});
}
console.log(pos);
The first log shows the following: {lng: -0.76##########, lat: 51.6#########}
which is perfect.
However the second log 'pos' is showing as nothing. Literally a blank line in the console.
I'm really hoping that I have made a really obvious and silly mistake, one of those head slapping moments because I cannot figure out why 'pos' is not coming through the scope of the function
I've researched into anonymous functions, callback, variable scope, and I cannot solve this. Please help.
N.B: I have a feeling the second log is being called before the first which would make the function call 'asynchronous'? But I don't know how to make that work for me.