Add basic location store for getting geoposition
This commit is contained in:
parent
beb6d9d066
commit
332e64c9ab
3 changed files with 64 additions and 1 deletions
35
ts/store/location.ts
Normal file
35
ts/store/location.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { defineStore } from "pinia";
|
||||
|
||||
interface GeolocationOptions {
|
||||
maximumAge?: number;
|
||||
timeout?: number;
|
||||
enableHighAccuracy?: boolean;
|
||||
}
|
||||
export const useLocationStore = defineStore("location", () => {
|
||||
function get(options?: GeolocationOptions): Promise<GeolocationPosition> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Check if geolocation is supported by the browser
|
||||
if (!navigator.geolocation) {
|
||||
reject(new Error("Geolocation is not supported by your browser"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Default options if none provided
|
||||
const geolocationOptions = options || {
|
||||
enableHighAccuracy: true,
|
||||
timeout: 5000,
|
||||
maximumAge: 0,
|
||||
};
|
||||
|
||||
// Call the geolocation API
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(position) => resolve(position),
|
||||
(error) => reject(error),
|
||||
geolocationOptions,
|
||||
);
|
||||
});
|
||||
}
|
||||
return {
|
||||
get,
|
||||
};
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue