javascript 检查是否允许地理定位并获取 Lat Lon
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10077606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if Geolocation was allowed and get Lat Lon
提问by Stefan
I'm building a small script that clients will install on their page. Geolocation is not necessary for it, however if it exists it would be nice to have. Is there a way for me to check if the clients page has requested geolocation information and if the user selected allow get the lat & lon without creating another prompt?
我正在构建一个客户端将安装在其页面上的小脚本。地理定位对它来说不是必需的,但是如果它存在,它会很好。有没有办法让我检查客户页面是否请求了地理位置信息,以及用户是否选择允许在不创建另一个提示的情况下获取纬度和经度?
回答by Endless
It isn't possible with the geolocation API but it is possible with the new permission API
地理定位 API 不可能,但新的权限 API 是可能的
This code is useful if you want to try to receive the coordinates but don't want to bother the user with a prompt dialog cuz the coordinates is not that important and may as well fallback to geoIP
如果您想尝试接收坐标但不想用提示对话框打扰用户,则此代码很有用,因为坐标不是那么重要,也可能回退到 geoIP
function getCoords() {
return new Promise((resolve, reject) =>
navigator.permissions ?
// Permission API is implemented
navigator.permissions.query({
name: 'geolocation'
}).then(permission =>
// is geolocation granted?
permission.state === "granted"
? navigator.geolocation.getCurrentPosition(pos => resolve(pos.coords))
: resolve(null)
) :
// Permission API was not implemented
reject(new Error("Permission API is not supported"))
)
}
getCoords().then(coords => console.log(coords))
This will resolve to null if permission hasn't been granted, cast a error if some javascript code wasn't supported by the browser and resolve to the coordinates if it has been granted
如果未授予权限,这将解析为 null,如果浏览器不支持某些 javascript 代码,则抛出错误,如果已授予权限,则解析为坐标
回答by tkone
You can detect if the feature exists by examining the window object.
您可以通过检查窗口对象来检测该功能是否存在。
if('geolocation' in window){
navigator.geolocation.getCurrentPosition(...);
}
This will cause a single user prompt to come up asking the user if they wish to share their location with your page.
这将导致出现单个用户提示,询问用户是否希望与您的页面共享他们的位置。
If geolocation is not available in the browser, this will not run at all.
如果浏览器中的地理定位不可用,则根本不会运行。
EDIT编辑如果您已经就此提示用户page load页面加载以允许地理定位访问,它不会再次提示您。如果用户离开并返回此页面,它会重新提示他们。
You may make subsequent calls to the API without prompting during that page session.
您可以在该页面会话期间不提示地对 API 进行后续调用。
回答by ThiefMaster
According to the APIit is not possible to find out of the user has already permitted the current site to retrieve the user's location without causing a prompt if he hasn't.See Endless answer
根据API,不可能发现用户已经允许当前站点检索用户的位置而不引起提示,如果他还没有。见无尽的答案
I'd suggest you to allow users to pass an argument when embedding your script telling it if it should use geolocation features or not.
我建议您允许用户在嵌入脚本时传递一个参数,告诉它是否应该使用地理定位功能。
回答by Fendi Septiawan
I also encountered the same problem. And, after I search and experiment I finally found the answer.
我也遇到了同样的问题。而且,经过我的搜索和实验,我终于找到了答案。
You can add this JS code :
您可以添加此 JS 代码:
navigator.permissions.query({name:'geolocation'}).then(function(result) {
// Will return ['granted', 'prompt', 'denied']
console.log(result.state);
});
Then you can use your custom code as needed.
然后您可以根据需要使用您的自定义代码。
source : https://developer.mozilla.org/en-US/docs/Web/API/Navigator/permissions
来源:https: //developer.mozilla.org/en-US/docs/Web/API/Navigator/permissions
回答by OsamaBinLogin
Whether or not you're prompted seems to depend on the browser and what 'privacy' settings it's got. Safari 6 has 3 settings: prompt once per site, prompt once per site per day, and deny. Chrome 23 has 3 settings: allow all, ask, and deny all. Sure would get good if there was a way to check if it's already allowed without bothering the user.
是否提示您似乎取决于浏览器以及它的“隐私”设置。Safari 6 有 3 种设置:每个站点提示一次、每个站点每天提示一次和拒绝。Chrome 23 有 3 个设置:全部允许、询问和全部拒绝。如果有一种方法可以在不打扰用户的情况下检查它是否已经被允许,那么肯定会变得很好。