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,12 +121,12 @@ 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" })
.then((status) => {
const self = this const self = this
console.log("Got geolocation state", status.state)
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) => {
@ -127,8 +136,6 @@ export class GeoLocationState {
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)
} }