简体   繁体   English

检查是否允许地理定位并获取 Lat Lon

[英]Check if Geolocation was allowed and get Lat Lon

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?有没有办法让我检查客户端页面是否请求了地理位置信息,以及用户是否选择了允许在不创建另一个提示的情况下获取纬度和经度?

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 代码,则抛出错误,如果已授予权限,则解析为坐标

You can detect if the feature exists by examining the window object.您可以通过检查 window object 来检测该功能是否存在。

if ('geolocation' in navigator) {
     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** If you've already prompted the user on this *page load* for allowing geolocation access, it will NOT prompt you again. **编辑** 如果您已经在此*页面加载*上提示用户允许地理定位访问,它不会再次提示您。 If the user navigates away and back to this page, it will re-prompt them. 如果用户离开并返回此页面,它将重新提示他们。

You may make subsequent calls to the API without prompting during that page session.您可以在该页面 session 期间不提示而拨打 API。

Reference: MDN Geolocation -- Watching the current position参考: MDN Geolocation -- Watching the current position

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

According to the API it 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.根据API ,不可能发现用户已经允许当前站点检索用户的位置,如果他没有,则不会引起提示。 See Endless answer见无尽的答案

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.我建议您在嵌入脚本时允许用户传递一个参数,告诉它是否应该使用地理定位功能。

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. Safari 6 有 3 个设置:每个站点提示一次,每天每个站点提示一次,拒绝。 Chrome 23 has 3 settings: allow all, ask, and deny all. Chrome 23 有 3 种设置:全部允许、询问和拒绝全部。 Sure would get good if there was a way to check if it's already allowed without bothering the user.如果有一种方法可以在不打扰用户的情况下检查它是否已经被允许,那肯定会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM