The issue appears to be due to Chrome blocking unsafe user location queries, so I've posted a question on Meta SE.
In stack snippet I am testing the code from https://developer.mozilla.org/es/docs/Web/API/Geolocation/getCurrentPosition
When I run it, the stack snippet console shows me
Warning: ERROR(1): User denied Geolocation
In my browser settings I have given permission to show my location to es.stackoverflow.com. What else am I missing?
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log('Latitude : ' + crd.latitude);
console.log('Longitude: ' + crd.longitude);
console.log('More or less ' + crd.accuracy + ' meters.');
};
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
};
navigator.geolocation.getCurrentPosition(success, error, options);
Stack snippets run in a sandbox , so they don't have permission to get the location, and there is no option the user can enable to allow it.
Specifically, it's because Stack Overflow doesn't set the flag
allow-same-origin
. That iswhat in your post is like:
It should be like this so that the location can be obtained:
Whether it is added or not is a complex discussion for MSE , which has already been consulted on occasion. Meanwhile, it is by design .
Alternatives:
Ask the user to run this in the console before running the snippet: