Vue function that tracks geolocation state of user's device, based on the Geolocation API.
interface UseGeolocation {
loading: boolean
accuracy: number | null
altitude: number | null
altitudeAccuracy: number | null
heading: number | null
latitude: number | null
longitude: number | null
speed: number | null
timestamp: number | null
error?: Error | PositionError
}
function useGeolocation(
options?: PositionOptions,
runOnMount?: boolean
): {
isTracking: Ref<boolean>;
geo: Ref<UseGeolocation>;
start: () => void;
stop: () => void;
}
options: PositionOptions
the geolocation position optionsrunOnMount: boolean
whether to run the geolocation tracking on mount,true
by default
geo: Ref<UseGeolocation>
the geolocation objectisTracking: Ref<boolean>
whether this function events are running or notstart: Function
the function used for starting the geolocation trackingstop: Function
the function used for stopping the geolocation tracking
<template>
<div>
<div>
Geo:
<pre>{{ JSON.stringify(geo, null, 2) }}</pre>
</div>
<button @click="start" v-if="!isTracking">
Enable geolocation tracking
</button>
<button @click="stop" v-else>Disable geolocation tracking</button>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { useGeolocation } from 'vue-use-kit'
export default Vue.extend({
name: 'UseGeolocationDemo',
setup() {
const { isTracking, geo, start, stop } = useGeolocation()
return { isTracking, geo, start, stop }
}
})
</script>