forked from MapComplete/MapComplete
Android: setup data bridge, polyfill geolocation
This commit is contained in:
parent
17450deb82
commit
76e9381650
23 changed files with 187 additions and 106 deletions
|
|
@ -3,7 +3,6 @@ import { LocalStorageSource } from "../Web/LocalStorageSource"
|
|||
import { QueryParameters } from "../Web/QueryParameters"
|
||||
import { Translation } from "../../UI/i18n/Translation"
|
||||
import Translations from "../../UI/i18n/Translations"
|
||||
import { Geolocation } from "@capacitor/geolocation"
|
||||
|
||||
export type GeolocationPermissionState = "prompt" | "requested" | "granted" | "denied"
|
||||
|
||||
|
|
@ -179,20 +178,19 @@ export class GeoLocationState {
|
|||
this.permission.setData("requested")
|
||||
try {
|
||||
const status = await navigator?.permissions?.query({ name: "geolocation" })
|
||||
const self = this
|
||||
console.log("Got geolocation state", status.state)
|
||||
if (status.state === "granted" || status.state === "denied") {
|
||||
self.permission.setData(status.state)
|
||||
self.startWatching()
|
||||
this.permission.setData(status.state)
|
||||
this.startWatching()
|
||||
return
|
||||
}
|
||||
status.addEventListener("change", () => {
|
||||
self.permission.setData(status.state)
|
||||
this.permission.setData(status.state)
|
||||
})
|
||||
// The code above might have reset it to 'prompt', but we _did_ request permission!
|
||||
this.permission.setData("requested")
|
||||
// We _must_ call 'startWatching', as that is the actual trigger for the popup...
|
||||
self.startWatching()
|
||||
this.startWatching()
|
||||
} catch (e) {
|
||||
console.error("Could not get permission:", e)
|
||||
}
|
||||
|
|
@ -203,46 +201,29 @@ export class GeoLocationState {
|
|||
* @private
|
||||
*/
|
||||
private async startWatching() {
|
||||
console.log("Starts watching", navigator.geolocation, Geolocation)
|
||||
const self = this
|
||||
try {
|
||||
await Geolocation.requestPermissions({ permissions: ["location"] })
|
||||
console.log("Requested permission")
|
||||
} catch (e) {
|
||||
// pass
|
||||
}
|
||||
try {
|
||||
await Geolocation.watchPosition(
|
||||
{
|
||||
enableHighAccuracy: true,
|
||||
maximumAge: 120000,
|
||||
},
|
||||
(position: GeolocationPosition, error: GeolocationPositionError) => {
|
||||
if (error) {
|
||||
if (error.code === 2 || error.code === 3) {
|
||||
self._gpsAvailable.set(false)
|
||||
return
|
||||
}
|
||||
self._gpsAvailable.set(true) // We go back to the default assumption that the location is physically available
|
||||
if (error.code === 1) {
|
||||
self.permission.set("denied")
|
||||
self._grantedThisSession.setData(false)
|
||||
return
|
||||
}
|
||||
console.warn("Could not get location with navigator.geolocation due to", error)
|
||||
}
|
||||
|
||||
|
||||
console.log("Got position:", position, JSON.stringify(position))
|
||||
if (!position) {
|
||||
return
|
||||
}
|
||||
this._gpsAvailable.set(true)
|
||||
this.currentGPSLocation.setData(position.coords)
|
||||
this._previousLocationGrant.setData(true)
|
||||
})
|
||||
} catch (e) {
|
||||
console.error("Could not get geolocation due to", e)
|
||||
}
|
||||
navigator.geolocation.watchPosition(
|
||||
(position: GeolocationPosition) => {
|
||||
this._gpsAvailable.set(true)
|
||||
this.currentGPSLocation.setData(position.coords)
|
||||
this._previousLocationGrant.setData(true)
|
||||
},
|
||||
(e: GeolocationPositionError) => {
|
||||
if (e.code === 2 || e.code === 3) {
|
||||
this._gpsAvailable.set(false)
|
||||
return
|
||||
}
|
||||
this._gpsAvailable.set(true) // We go back to the default assumption that the location is physically available
|
||||
if (e.code === 1) {
|
||||
this.permission.set("denied")
|
||||
this._grantedThisSession.setData(false)
|
||||
return
|
||||
}
|
||||
console.warn("Could not get location with navigator.geolocation due to", e)
|
||||
},
|
||||
{
|
||||
enableHighAccuracy: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue