メインコンテンツにスキップ
プラグインに戻る
@capgo/capacitor-compass
チュートリアル
by github.com/Cap-go

コンパス

デバイスのコンパス向きを読み取り、連続的な更新とパーミッションの管理で、度単位で表示します。

ガイド

コンパスについてのチュートリアル

@capgo/capacitor-compass を使用

Capacitor コンパス プラグインは、デバイスのコンパス向きを読み取るためのインターフェースです。

インストール

bun add @capgo/capacitor-compass
bunx cap sync

このプラグインが公開するもの

  • getCurrentHeading - iOSではバックグラウンドで、Androidでは加速度計と磁気センサを使用して呼び出されたときに計算される角度を取得します。
  • startListening - コンパスヘッディングの変更をイベントを介してリスンすることができます。このメソッドを呼び出すと、コンパスセンサを開始し、'headingChange'イベントを発行します。
  • stopListening - コンパスヘッディングの変更をリスンすることを停止します。このメソッドを呼び出すと、コンパスセンサを停止し、イベントの発行を停止します。
  • checkPermissions - iOSでは位置情報の許可状況をチェックし、Androidでは常に'granted'を返します。

使用例

getCurrentHeading

iOSではバックグラウンドで、Androidでは加速度計と磁気センサを使用して呼び出されたときに計算される角度を取得します。

import { CapgoCompass } from '@capgo/capacitor-compass';

const { value } = await CapgoCompass.getCurrentHeading();
console.log('Compass heading:', value, 'degrees');

startListening

コンパスヘッディングの変更をイベントを介してリスンすることができます。このメソッドを呼び出すと、コンパスセンサを開始し、'headingChange'イベントを発行します。

import { CapgoCompass } from '@capgo/capacitor-compass';

// With default throttling (100ms interval, 2° minimum change)
await CapgoCompass.startListening();

// With custom throttling for high-frequency updates
await CapgoCompass.startListening({
  minInterval: 50,      // 50ms between events
  minHeadingChange: 1.0 // 1° minimum change
});

CapgoCompass.addListener('headingChange', (event) => {
  console.log('Heading:', event.value);
});

stopListening

コンパスヘッディングの変更をリスンすることを停止します。このメソッドを呼び出すと、コンパスセンサを停止し、イベントの発行を停止します。

import { CapgoCompass } from '@capgo/capacitor-compass';

await CapgoCompass.stopListening();

checkPermissions

iOSでは位置情報の許可状況をチェックし、Androidでは常に'granted'を返します。

import { CapgoCompass } from '@capgo/capacitor-compass';

const status = await CapgoCompass.checkPermissions();
console.log('Compass permission:', status.compass);

フルリファレンス

@capgo/capacitor-compass

Capgoを使用している場合 Capgoを使用してダッシュボードとCapgoのオペレーションを計画する場合、@capgo/capacitor-compass @API/__CAPGO_KEEP_1__-compass @capgo/capacitor-compass for the implementation detail in @capgo/capacitor-compass, Getting Startedの実装詳細 Capgoの概要 API Overview for the implementation detail in API Overview, Introduction for the implementation detail in Introduction, and API Keys for the implementation detail in API Keys.