コンテンツへスキップ

useGeolocation

カテゴリ
エクスポートサイズ
526 B
最終変更
先週

リアクティブなGeolocation API。ユーザーが希望する場合、Webアプリケーションに自分の位置情報を提供できます。プライバシー上の理由から、ユーザーは位置情報を報告する許可を求められます。

デモ

{
  "coords": {
    "accuracy": 0,
    "latitude": null,
    "longitude": null,
    "altitude": null,
    "altitudeAccuracy": null,
    "heading": null,
    "speed": null
  },
  "locatedAt": null,
  "error": null
}

使い方

js
import { useGeolocation } from '@vueuse/core'

const { coords, locatedAt, error, resume, pause } = useGeolocation()
状態タイプ説明
coords座標緯度や経度など、取得した位置情報に関する情報
locatedAt日付最後の地理位置情報呼び出しの時間
errorstring地理位置情報APIが失敗した場合のエラーメッセージ。
resume関数地理位置情報の更新を再開する制御関数
pause関数地理位置情報の更新を一時停止する制御関数

設定

useGeolocation関数は、オプションのパラメータとしてPositionOptionsオブジェクトを受け取ります。

コンポーネントの使い方

この関数は、@vueuse/componentsパッケージを通じてレンダリングレスコンポーネントバージョンも提供します。使い方の詳細はこちら

vue
<template>
  <UseGeolocation v-slot="{ coords: { latitude, longitude } }">
    Latitude: {{ latitude }}
    Longitude: {{ longitude }}
  </UseGeolocation>
</template>

型宣言

typescript
export interface UseGeolocationOptions
  extends Partial<PositionOptions>,
    ConfigurableNavigator {
  immediate?: boolean
}
/**
 * Reactive Geolocation API.
 *
 * @see https://vueuse.dokyumento.jp/useGeolocation
 * @param options
 */
export declare function useGeolocation(options?: UseGeolocationOptions): {
  isSupported: ComputedRef<boolean>
  coords: Ref<
    Omit<GeolocationCoordinates, "toJSON">,
    Omit<GeolocationCoordinates, "toJSON">
  >
  locatedAt: Ref<number | null, number | null>
  error: ShallowRef<
    GeolocationPositionError | null,
    GeolocationPositionError | null
  >
  resume: () => void
  pause: () => void
}
export type UseGeolocationReturn = ReturnType<typeof useGeolocation>

ソース

ソースデモドキュメント

貢献者

Anthony Fu
Antério Vieira
Jelf
Anthony Fu
丶遠方
vaakian X
Enric Bisbe Gil
Egor Startsev
Michael J. Roberts
死神
小麦
Alex Kozack
Sanxiaozhizi

変更履歴

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2 のサポートを廃止、バンドルを最適化し、クリーンアップ (#4349)
v9.11.0 2023/01/17
d5321 - fix(components): defineComponent を pure としてマーク (#2623)
v9.7.0 2022/12/16
161fd - fix: immediate オプションを実装 (#2472)
v9.5.0 2022/11/09
2c4fd - feat: 一時停止と再開のコントロール (#2376)

MITライセンスの下でリリースされています。