📄 useGeolocation¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 4 |
| 📦 Imports | 7 |
| 📐 Interfaces | 2 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/useGeolocation/index.ts
📦 Imports¶
| Name | Source |
|---|---|
ShallowRef |
vue |
ConfigurableNavigator |
../_configurable |
Supportable |
../types |
tryOnScopeDispose |
@vueuse/shared |
shallowRef |
vue |
defaultNavigator |
../_configurable |
useSupported |
../useSupported |
Functions¶
useGeolocation(options: UseGeolocationOptions): UseGeolocationReturn¶
Reactive Geolocation API.
Parameters:
optionsany: No description
See: https://vueuse.org/useGeolocation
Raw JSDoc
Calls:
useSupported (from ../useSupported)shallowRef (from vue)navigator!.geolocation.watchPositionresumenavigator.geolocation.clearWatchtryOnScopeDispose (from @vueuse/shared)pause
Code
export function useGeolocation(options: UseGeolocationOptions = {}): UseGeolocationReturn {
const {
enableHighAccuracy = true,
maximumAge = 30000,
timeout = 27000,
navigator = defaultNavigator,
immediate = true,
} = options
const isSupported = useSupported(() => navigator && 'geolocation' in navigator)
const locatedAt = shallowRef<number | null>(null)
const error = shallowRef<GeolocationPositionError | null>(null)
const coords = shallowRef<Omit<GeolocationPosition['coords'], 'toJSON'>>({
accuracy: 0,
latitude: Number.POSITIVE_INFINITY,
longitude: Number.POSITIVE_INFINITY,
altitude: null,
altitudeAccuracy: null,
heading: null,
speed: null,
})
function updatePosition(position: GeolocationPosition) {
locatedAt.value = position.timestamp
coords.value = position.coords
error.value = null
}
let watcher: number
function resume() {
if (isSupported.value) {
watcher = navigator!.geolocation.watchPosition(
updatePosition,
err => error.value = err,
{
enableHighAccuracy,
maximumAge,
timeout,
},
)
}
}
if (immediate)
resume()
function pause() {
if (watcher && navigator)
navigator.geolocation.clearWatch(watcher)
}
tryOnScopeDispose(() => {
pause()
})
return {
isSupported,
coords,
locatedAt,
error,
resume,
pause,
}
}
Internal helpers¶
Declared inside another function in this file.
updatePosition(position: GeolocationPosition): void¶
Parameters:
positionGeolocationPosition
Returns: void
Code
resume(): void¶
Returns: void
Calls:
navigator!.geolocation.watchPosition
Code
pause(): void¶
Returns: void
Calls:
navigator.geolocation.clearWatch
Interfaces¶
UseGeolocationOptions¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
immediate |
boolean |
✓ | not shown |
UseGeolocationReturn¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
coords |
ShallowRef<Omit<GeolocationPosition['coords'], 'toJSON'>> |
✗ | not shown |
locatedAt |
ShallowRef<number \| null> |
✗ | not shown |
error |
ShallowRef<GeolocationPositionError \| null> |
✗ | not shown |
resume |
() => void |
✗ | not shown |
pause |
() => void |
✗ | not shown |
Generated by Syntax Scribe