This commit is contained in:
Pieter Vander Vennet 2023-09-30 15:30:11 +02:00
parent a7a9790795
commit 0727a228fd

View file

@ -99,6 +99,15 @@ export class GeoLocationState {
* This class will start watching * This class will start watching
*/ */
public requestPermission() { public requestPermission() {
this.requestPermissionAsync()
}
/**
* Requests the user to allow access to their position.
* When granted, will be written to the 'geolocationState'.
* This class will start watching
*/
public async requestPermissionAsync() {
if (typeof navigator === "undefined") { if (typeof navigator === "undefined") {
// Not compatible with this browser // Not compatible with this browser
this.permission.setData("denied") this.permission.setData("denied")
@ -112,23 +121,21 @@ export class GeoLocationState {
this.permission.setData("requested") this.permission.setData("requested")
try { try {
navigator?.permissions const status = await navigator?.permissions?.query({ name: "geolocation" })
?.query({ name: "geolocation" }) const self = this
.then((status) => { console.log("Got geolocation state", status.state)
const self = this if (status.state === "granted" || status.state === "denied") {
if (status.state === "granted" || status.state === "denied") { self.permission.setData(status.state)
self.permission.setData(status.state) self.startWatching()
return return
} }
status.addEventListener("change", (e) => { status.addEventListener("change", (e) => {
self.permission.setData(status.state) self.permission.setData(status.state)
}) })
// The code above might have reset it to 'prompt', but we _did_ request permission! // The code above might have reset it to 'prompt', but we _did_ request permission!
this.permission.setData("requested") this.permission.setData("requested")
// We _must_ call 'startWatching', as that is the actual trigger for the popup... // We _must_ call 'startWatching', as that is the actual trigger for the popup...
self.startWatching() self.startWatching()
})
.catch((e) => console.error("Could not get geopermission", e))
} catch (e) { } catch (e) {
console.error("Could not get permission:", e) console.error("Could not get permission:", e)
} }