I just updated to Chrome 64 and started noticing this message in our webapp:
[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?
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?
The Geolocation method watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function.
The Navigator geolocation property is used to return a geolocation object by the browser which can be used to locate a user's position. It is a read-only property and is only available on the approval of the user since it can compromise the user's privacy.
It seems there's a recommendation from Google not to load geolocation on page load:
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()
:
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.
We also noticed that there's a massive delay in getting the geolocation of user since that Chrome update.
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.
id = navigator.geolocation.watchPosition(success[, error[, options]])
The violation is not related to polling. It is caused by attempting to access geolocation on page load. Geolocation access should only be requested after a user "gesture" like a click or tap.
document.querySelector('.permission-granted-button').addEventListener('click', () => {
navigator.geolocation.watchPosition(successCallback, errorCallback, optionsObject);
});
https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With