Skip to content

Commit

Permalink
added pause and resume controls for geolocation watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
egstartsev committed Oct 28, 2022
1 parent 39b1f00 commit 592e87a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
8 changes: 4 additions & 4 deletions packages/core/useGeolocation/demo.vue
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { useGeolocation } from '@vueuse/core'
const { coords, locatedAt, error, stop, start } = useGeolocation()
const { coords, locatedAt, error, resume, pause } = useGeolocation()
</script>

<template>
Expand All @@ -25,10 +25,10 @@ const { coords, locatedAt, error, stop, start } = useGeolocation()
2,
)
}}</pre>
<button @click="stop">
Stop watch
<button @click="pause">
Pause watch
</button>
<button @click="start">
<button @click="resume">
Start watch
</button>
</div>
Expand Down
4 changes: 3 additions & 1 deletion packages/core/useGeolocation/index.md
Expand Up @@ -11,14 +11,16 @@ Reactive [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geol
```js
import { useGeolocation } from '@vueuse/core'

const { coords, locatedAt, error } = useGeolocation()
const { coords, locatedAt, error, start, stop } = useGeolocation()
```

| State | Type | Description |
| --------- | ----------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
| coords | [`Coordinates`](https://developer.mozilla.org/en-US/docs/Web/API/Coordinates) | information about the position retrieved like the latitude and longitude |
| locatedAt | `Date` | The time of the last geolocation call |
| error | `string` | An error message in case geolocation API fails. |
| resume | `function` | Control function to resume updating geolocation |
| pause | `function` | Control function to pause updating geolocation |

## Config

Expand Down
36 changes: 24 additions & 12 deletions packages/core/useGeolocation/index.ts
Expand Up @@ -7,7 +7,9 @@ import type { ConfigurableNavigator } from '../_configurable'
import { defaultNavigator } from '../_configurable'
import { useSupported } from '../useSupported'

export interface UseGeolocationOptions extends Partial<PositionOptions>, ConfigurableNavigator {}
export interface UseGeolocationOptions extends Partial<PositionOptions>, ConfigurableNavigator {
immediate?: boolean
}

/**
* Reactive Geolocation API.
Expand Down Expand Up @@ -45,28 +47,38 @@ export function useGeolocation(options: UseGeolocationOptions = {}) {

let watcher: number

if (isSupported.value) {
watcher = navigator!.geolocation.watchPosition(
updatePosition,
err => error.value = err,
{
enableHighAccuracy,
maximumAge,
timeout,
},
)
function resume() {
if (isSupported.value) {
watcher = navigator!.geolocation.watchPosition(
updatePosition,
err => error.value = err,
{
enableHighAccuracy,
maximumAge,
timeout,
},
)
}
}

tryOnScopeDispose(() => {
resume()

function pause() {
if (watcher && navigator)
navigator.geolocation.clearWatch(watcher)
}

tryOnScopeDispose(() => {
pause()
})

return {
isSupported,
coords,
locatedAt,
error,
resume,
pause,
}
}

Expand Down

0 comments on commit 592e87a

Please sign in to comment.