mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-10-20 23:11:24 +02:00
feat(geolocation): support for non-GMS devices (#3377)
* chore(geolocation): update gradle * feat(geolocation): add support for android devices without GMS * fix(geolocation): watchPosition does not resolve on android * feat(example): add watch support for geolocation * chore: add changeset * Update .changes/geolocation-non-gms.md Co-authored-by: Fabian-Lars <30730186+FabianLars@users.noreply.github.com> * fix(geolocation): only use Android Location API on API Level >= 31 * fix resolve * fix(geolocation): use LocationManagerCompat for non-GMS devices - support API 24+ instead of only API 31+ - pick the best enabled provider (fused, gps, network) instead of GPS only - honor enableHighAccuracy in getCurrentPosition - clear the previous listener before starting a new watch - report an error when the provider is disabled during a watch - share the provider/priority logic between both code paths - drop the geolocation-js bump, no JS changes * fix(examples): clear existing geolocation watch before starting a new one * chore(geolocation): add change file for watchPosition resolve fix * fix(geolocation): honor enableHighAccuracy in getCurrentPosition on Android 12+ without GMS * fix(geolocation): fall back to the next provider when one returns no fix on Android without GMS * fix(geolocation): honor timeout in getCurrentPosition on Android * fix(geolocation): keep multiple watchPosition watches active on Android * chore(geolocation): bump geolocation-js alongside geolocation in change files * refactor(geolocation): report provider errors from the callers of androidProviders * e2e tests --------- Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> Co-authored-by: Fabian-Lars <30730186+FabianLars@users.noreply.github.com> Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
15 files changed
+573
-64
No files matched your search
@@ -2,11 +2,18 @@
|
||||
import {
|
||||
checkPermissions,
|
||||
requestPermissions,
|
||||
getCurrentPosition
|
||||
getCurrentPosition,
|
||||
|
||||
watchPosition,
|
||||
clearWatch
|
||||
|
||||
} from '@tauri-apps/plugin-geolocation'
|
||||
|
||||
export let onMessage
|
||||
|
||||
let pos = null
|
||||
let watchId = null
|
||||
|
||||
async function getPosition() {
|
||||
let permissions = await checkPermissions()
|
||||
if (
|
||||
@@ -17,13 +24,74 @@
|
||||
}
|
||||
|
||||
if (permissions.location === 'granted') {
|
||||
getCurrentPosition().then(onMessage).catch(onMessage)
|
||||
getCurrentPosition().then((position) => {
|
||||
pos = position
|
||||
onMessage(position)
|
||||
}).catch((err) => {
|
||||
pos = null
|
||||
onMessage(err)
|
||||
})
|
||||
} else {
|
||||
onMessage('permission denied')
|
||||
}
|
||||
}
|
||||
|
||||
async function watchPos() {
|
||||
let permissions = await checkPermissions()
|
||||
if (
|
||||
permissions.location === 'prompt' ||
|
||||
permissions.location === 'prompt-with-rationale'
|
||||
) {
|
||||
permissions = await requestPermissions(['location'])
|
||||
}
|
||||
|
||||
if (permissions.location === 'granted') {
|
||||
await stopWatching()
|
||||
watchId = await watchPosition({
|
||||
enableHighAccuracy: true,
|
||||
timeout: 5000,
|
||||
maximumAge: 0
|
||||
}, (position) => {
|
||||
pos = position
|
||||
onMessage(position)
|
||||
})
|
||||
onMessage('watchId: ' + watchId)
|
||||
} else {
|
||||
onMessage('permission denied')
|
||||
}
|
||||
}
|
||||
|
||||
async function stopWatching() {
|
||||
if (watchId === null) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
await clearWatch(watchId)
|
||||
} catch (err) {
|
||||
onMessage(err)
|
||||
}
|
||||
watchId = null
|
||||
pos = null
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<button class="btn" id="cli-matches" on:click={getPosition}>
|
||||
Get Position
|
||||
</button>
|
||||
|
||||
<button class="btn" on:click={watchPos}>
|
||||
Watch Position
|
||||
</button>
|
||||
|
||||
<button class="btn" on:click={stopWatching}>
|
||||
Stop Watching
|
||||
</button>
|
||||
|
||||
{#if watchId}
|
||||
<span>Watch ID: {watchId}</span>
|
||||
{/if}
|
||||
|
||||
{#if pos}
|
||||
<pre>{JSON.stringify(pos, null, 2)}</pre>
|
||||
{/if}
|
||||
Reference in new issue
Block a user