最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - navigator.getCurrentPosition() not working in Firefox nor Safari - Stack Overflow

programmeradmin0浏览0评论

I can't seem to get navigator.getLocation() working on either Firefox 65 or Safari 10

function getLocation(user_radius) {

var geo_options = {
enableHighAccuracy: true, 
maximumAge        : 30000, 
timeout           : 27000
};

function error(err) {
console.warn('Cannot load user location. Make sure you gave permission to share location');
}

document.body.style.position = "absolute";

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {

        lat0 = position.coords.latitude;
        long0 = position.coords.longitude;
        corefunction(user_radius);
    },error,geo_options);
} else {
    x.innerHTML = "Geolocation is not supported by this browser.";
}
}

Although Firefox ask to access location while Safari doesn't, neither of them return latitude nor longitude. Firefox and Safari log:

*Cannot load user location. Make sure you gave.....*

what am I doing wrong?

Yes I gave permissions, and yes I have location services enabled in both. The code works fine in Opera, Chrome, IE and Edge...

The curious thing is that I canNOT get my location with those browser even with 3rd party pages:

.asp?filename=tryhtml5_geolocation

UPDATE#1 I have tried Steven's suggestion to add navigator.permission;This is the snipplet I ve run just to test if it worked in Firefox.

   function handlePermission() {
  navigator.permissions.query({name:'geolocation'}).then(function(result) {
  if (result.state == 'granted') {
  report(result.state);
  geoBtn.style.display = 'none';
} else if (result.state == 'prompt') {
  report(result.state);
  geoBtn.style.display = 'none';
  navigator.geolocation.getCurrentPosition(function (position) {

        lat0 = position.coords.latitude;
        long0 = position.coords.longitude;

    },error,geo_options);
} else if (result.state == 'denied') {
  report(result.state);
  geoBtn.style.display = 'inline';
}
result.onchange = function() {
  report(result.state);
}
});
}

The console says "prompt" but getCurrentPosition() is not executed.

I can't seem to get navigator.getLocation() working on either Firefox 65 or Safari 10

function getLocation(user_radius) {

var geo_options = {
enableHighAccuracy: true, 
maximumAge        : 30000, 
timeout           : 27000
};

function error(err) {
console.warn('Cannot load user location. Make sure you gave permission to share location');
}

document.body.style.position = "absolute";

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {

        lat0 = position.coords.latitude;
        long0 = position.coords.longitude;
        corefunction(user_radius);
    },error,geo_options);
} else {
    x.innerHTML = "Geolocation is not supported by this browser.";
}
}

Although Firefox ask to access location while Safari doesn't, neither of them return latitude nor longitude. Firefox and Safari log:

*Cannot load user location. Make sure you gave.....*

what am I doing wrong?

Yes I gave permissions, and yes I have location services enabled in both. The code works fine in Opera, Chrome, IE and Edge...

The curious thing is that I canNOT get my location with those browser even with 3rd party pages:

https://www.w3schools./html/tryit.asp?filename=tryhtml5_geolocation https://developer.mozilla/en-US/docs/Web/API/Geolocation_API

UPDATE#1 I have tried Steven's suggestion to add navigator.permission;This is the snipplet I ve run just to test if it worked in Firefox.

   function handlePermission() {
  navigator.permissions.query({name:'geolocation'}).then(function(result) {
  if (result.state == 'granted') {
  report(result.state);
  geoBtn.style.display = 'none';
} else if (result.state == 'prompt') {
  report(result.state);
  geoBtn.style.display = 'none';
  navigator.geolocation.getCurrentPosition(function (position) {

        lat0 = position.coords.latitude;
        long0 = position.coords.longitude;

    },error,geo_options);
} else if (result.state == 'denied') {
  report(result.state);
  geoBtn.style.display = 'inline';
}
result.onchange = function() {
  report(result.state);
}
});
}

The console says "prompt" but getCurrentPosition() is not executed.

Share edited Mar 6, 2019 at 20:07 Mat Bravo asked Mar 6, 2019 at 18:25 Mat BravoMat Bravo 1311 silver badge8 bronze badges 12
  • you need to also implement the permissions API. the error is due to the user not allowing geolocation permissions. see: developer.mozilla/en-US/docs/Web/API/Permissions_API/… – Steven Stark Commented Mar 6, 2019 at 18:34
  • Thanks Steve, I have added the navigator.permission.query(...) from the page you linked. The log is showing "prompt" as a status but nothing is returned and It looks like getCurrentPosition() is not executed... – Mat Bravo Commented Mar 6, 2019 at 19:53
  • Could you update your code example? Are you using code similar to this page: developer.mozilla/en-US/docs/Web/API/PermissionStatus – Steven Stark Commented Mar 6, 2019 at 19:57
  • added to main post UPDATE#1 – Mat Bravo Commented Mar 6, 2019 at 20:08
  • I am not en expert on FireFox permissions API, but I might be able to help a bit more, but this is just a hunch at this point. In my experience, when a user 'denies' permission on a given domain at any point, they will not be prompted to re-allow permissions and instead need to navigate into the browser settings for the given domain to actually change this to 'allow', not even the prompt will show. This sounds to me like a similar situation, though I experienced it with mobile safari on iOS, – Steven Stark Commented Mar 6, 2019 at 20:15
 |  Show 7 more ments

1 Answer 1

Reset to default 10

Ok I found what was the problem and it was OSX and iOS. Firefox and Safari when running under those browsers needs location services habilitated from Finder->apple->System Preferences->Security & Privacy-> Privacy then add Firefox and Safari to the whitelist

For some reason, Chrome is setting itself into the whitelist after you accepted to share location on browser. The same for Opera.

Hence in a nutshell, the problem was Apple's privacy system which is a bit convoluted. Goods and bads I suppose

发布评论

评论列表(0)

  1. 暂无评论