Only request geolocation information in response to a user gesture?

JOATMON picture JOATMON · Nov 30, 2017 · Viewed 16.4k times · Source

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?

Answer

Stephan picture Stephan · Mar 21, 2018

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.