Only request geolocation information in response to a user gesture? Only request geolocation information in response to a user gesture? javascript javascript

Only request geolocation information in response to a user gesture?


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]])

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


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 HTTPS protocal not HTTP.