Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useGeolocation): pause and resume controls #2376

Merged
merged 2 commits into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/core/useGeolocation/demo.vue
@@ -1,11 +1,12 @@
<script setup lang="ts">
import { useGeolocation } from '@vueuse/core'

const { coords, locatedAt, error } = useGeolocation()
const { coords, locatedAt, error, resume, pause } = useGeolocation()
</script>

<template>
<pre lang="json">{{
<div>
<pre lang="json">{{
JSON.stringify(
{
coords: {
Expand All @@ -24,4 +25,11 @@ const { coords, locatedAt, error } = useGeolocation()
2,
)
}}</pre>
<button @click="pause">
Pause watch
</button>
<button @click="resume">
Start watch
</button>
</div>
</template>
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, resume, pause } = 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this doing anything? I don't see it being used

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the code in #2372

We are missing:

if (immediate)
    resume()

so resume() is not always directly called. I can make a PR as I need that feature.

}

/**
* 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