javascript 仅请求地理位置信息以响应用户手势?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47581575/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:28:40  来源:igfitidea点击:

Only request geolocation information in response to a user gesture?

javascriptgoogle-chrome-devtoolsuser-experience

提问by Devil's Advocate

I just updated to Chrome 64 and started noticing this message in our webapp:

我刚刚更新到 Chrome 64 并开始在我们的 web 应用程序中注意到此消息:

[Violation] Only request geolocation information in response to a user gesture.

This seems overly restrictive. I don't have to click "Update location" repeatedly when I'm navigating in Google Maps. What is the alternative to location polling?

这似乎过于严格了。在 Google 地图中导航时,我不必反复单击“更新位置”。位置轮询的替代方法是什么?

We are currently using the following code:

我们目前正在使用以下代码:

let locInt = self.setInterval(function(){
    navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);
},5000);

Our app is location based and relies on a constantly updated position. What is the preferred method here?

我们的应用程序基于位置并依赖于不断更新的位置。这里的首选方法是什么?

采纳答案by Kayce Basques

The watchPosition()method lets you register a handler that the browser calls automatically every time that the device's position changes. This is preferable to polling.

watchPosition()方法允许您注册一个处理程序,每次设备的位置更改时浏览器都会自动调用该处理程序。这比轮询更可取。

id = navigator.geolocation.watchPosition(success[, error[, options]])

https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition

https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition

回答by Stephan

It seems there's a recommendationfrom Google notto load geolocation on page load:

似乎Google建议不要在页面加载时加载地理位置:

Users are mistrustful of or confused by pages that automatically request their location on page load. Rather than automatically requesting a user's location on page load, tie the request to a user's gesture, such as a tapping a "Find Stores Near Me" button. Make sure that the gesture clearly and explicitly expresses the need for the user's location.

用户对在页面加载时自动请求其位置的页面不信任或感到困惑。不是在页面加载时自动请求用户的位置,而是将请求与用户的手势联系起来,例如点击“查找我附近的商店”按钮。确保手势清晰明确地表达了对用户位置的需求。

And it doesn't matter if you use watchPosition()or getCurrentPosition():

如果您使用watchPosition()or并不重要getCurrentPosition()

Lighthouse collects the JavaScript that was executed on page load. If this code contains calls to geolocation.getCurrentPosition() or geolocation.watchPosition(), and geolocation permission was not already granted, then the user's location was requested.

Lighthouse 收集在页面加载时执行的 JavaScript。如果此代码包含对 geolocation.getCurrentPosition() 或 geolocation.watchPosition() 的调用,并且尚未授予地理定位权限,则请求了用户的位置。

We also noticed that there's a massive delay in getting the geolocation of user since that Chrome update.

我们还注意到,自 Chrome 更新以来,获取用户的地理位置有很大的延迟。

回答by Hassan Asadi

I find the solution sort of! This problem exist becuase of "HTTP". If you want use javascript APIs like "geolocation" your server or local server must be HTTPSprotocal not HTTP.

我找到了解决方案!由于“HTTP”而存在此问题。如果您想使用像“地理定位”这样的 javascript API,您的服务器或本地服务器必须是HTTPS协议而不是 HTTP。