navigator.getCurrentPosition()在Firefox或Safari中均不起作用 [英] navigator.getCurrentPosition() not working in Firefox nor Safari

查看:204
本文介绍了navigator.getCurrentPosition()在Firefox或Safari中均不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法在Firefox 65或Safari 10上运行navigator.getLocation()

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.";
}
}

尽管Firefox要求访问位置,而Safari却不要求访问位置,但它们都不返回纬度或经度. Firefox和Safari日志:

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

我在做什么错了?

是的,我授予了权限,是的,我都启用了定位服务. 该代码可以在Opera,Chrome,IE和Edge中正常工作.

奇怪的是,即使在第三方页面中,我也无法通过这些浏览器获取位置:

https://www.w3schools.com/html/tryit.asp ?filename = tryhtml5_geolocation https://developer.mozilla.org/zh-CN/docs/Web/API/Geolocation_API

UPDATE#1 我已经尝试过史蒂文(Steven)的建议来添加navigator.permission;这是我运行的片段,目的是测试它是否可以在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);
}
});
}

控制台显示提示",但未执行getCurrentPosition().

解决方案

好,我发现了问题所在,这是OSX和iOS. 在这些浏览器下运行的Firefox和Safari需要通过Finder-> apple->系统偏好设置->安全性和隐私->隐私 然后将Firefox和Safari添加到白名单

由于某些原因,在您同意共享浏览器上的位置后,Chrome会将自己设置为白名单.对于Opera同样如此.

简而言之,问题在于苹果的隐私系统有些复杂.我想的是好事

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.com/html/tryit.asp?filename=tryhtml5_geolocation https://developer.mozilla.org/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.

解决方案

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

这篇关于navigator.getCurrentPosition()在Firefox或Safari中均不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆