Getting Started
このプラグインのインストール手順と全マークダウンガイドを含む設定用の質問をコピーします。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-compass`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/compass/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
インストール
「インストール」というタイトルのセクションCapgoのAI-Assisted Setupを使用してプラグインをインストールできます。AIツールにCapgoスキルを追加するには、以下のコマンドを実行してください。
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins次に、以下のプロンプトを使用してください。
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-compass` plugin in my project.Manual Setupを使用する場合は、以下のコマンドを実行してプラグインをインストールし、以下のプラットフォーム固有の指示に従ってください。
bun add @capgo/capacitor-compassbunx cap syncインポート
「インポート」のセクションimport { CapgoCompass } from '@capgo/capacitor-compass';API オーバービュー
「API オーバービュー」のセクションgetCurrentHeading
「getCurrentHeading」のセクション現在の方位角度を取得します。 iOSでは、背景で更新され、最新の値が返されます。 Androidでは、加速度計と磁気センサを使用してメソッドを呼び出すと、方位角度が計算されます。 Webでは実装されていません。
import { CapgoCompass } from '@capgo/capacitor-compass';
const { value } = await CapgoCompass.getCurrentHeading();console.log('Compass heading:', value, 'degrees');startListening
「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 updatesawait CapgoCompass.startListening({ minInterval: 50, // 50ms between events minHeadingChange: 1.0 // 1° minimum change});
CapgoCompass.addListener('headingChange', (event) => { console.log('Heading:', event.value);});stopListening
__CAPGO_KEEP_1__コンパスヘッディングの変更を待つ機能を停止します。 この機能はコンパスセンサを停止し、イベントの発行を停止します。
import { CapgoCompass } from '@capgo/capacitor-compass';
await CapgoCompass.stopListening();checkPermissions
__CAPGO_KEEP_1__コンパスデータへのアクセス権の現在の状態を確認します。 iOSでは、位置情報の許可状態を確認します。 Androidでは、このメソッドは常に「許可」状態を返します。なぜなら、許可が必要ないからです。
import { CapgoCompass } from '@capgo/capacitor-compass';
const status = await CapgoCompass.checkPermissions();console.log('Compass permission:', status.compass);requestPermissions
__CAPGO_KEEP_1__コンパスデータへのアクセス許可を要求します。 iOSでは、ヘッディングデータのために位置情報の許可を要求します。 Androidでは、このメソッドはすぐに解決します。なぜなら、許可が必要ないからです。
import { CapgoCompass } from '@capgo/capacitor-compass';
const status = await CapgoCompass.requestPermissions();if (status.compass === 'granted') { // Can now use compass}watchAccuracy
__CAPGO_KEEP_1__コンパス精度を監視します。 Androidでは、このメソッドは磁気センサの精度を監視し、精度の変更イベントを発行します。 開発者は、これらのイベントにリスナーを設定し、カリブレーション用のUIを実装できます。 iOSとWebでは、このメソッドは何も実行しません。なぜなら、コンパス精度の監視は利用できないからです。
import { CapgoCompass } from '@capgo/capacitor-compass';
// Start monitoring accuracyawait CapgoCompass.watchAccuracy();
// Listen for accuracy changes and implement custom UICapgoCompass.addListener('accuracyChange', (event) => { console.log('Accuracy changed to:', event.accuracy); if (event.accuracy < CompassAccuracy.MEDIUM) { // Show your custom calibration UI }});unwatchAccuracy
「unwatchAccuracy」セクションコンパス精度の監視を停止します。 精度の監視を停止します。
import { CapgoCompass } from '@capgo/capacitor-compass';
await CapgoCompass.unwatchAccuracy();getAccuracy
「getAccuracy」セクション現在のコンパス精度を取得します。 Androidでは、現在の磁気センサの精度を返します。 iOSとWebでは、精度の監視が利用できないため、常にCompassAccuracy.UNKNOWNを返します。
import { CapgoCompass } from '@capgo/capacitor-compass';
const { accuracy } = await CapgoCompass.getAccuracy();if (accuracy < CompassAccuracy.MEDIUM) { console.log('Compass needs calibration');}型の参照
「型の参照」セクションCompassHeading
「CompassHeading」セクションコンパス向きの値を含む結果
export interface CompassHeading { /** Compass heading in degrees (0-360) */ value: number;}ListeningOptions
リスニングオプションコンパスリスニングの設定を構成するためのオプション。
export interface ListeningOptions { /** * Minimum interval between heading change events in milliseconds. * Lower values = more frequent updates but higher CPU/battery usage. * * @default 100 * @since 8.1.4 */ minInterval?: number;
/** * Minimum heading change in degrees required to trigger an event. * Lower values = more sensitive but more events. * Handles wraparound (e.g., 359° to 1° = 2° change). * * @default 2.0 * @since 8.1.4 */ minHeadingChange?: number;}HeadingChangeEvent
ヘッディング変更イベントのデータクリップボードにコピー
export interface HeadingChangeEvent { /** Compass heading in degrees (0-360) */ value: number;}AccuracyChangeEvent
クリップボードにコピーコンパスプラグインの許可状態
export interface AccuracyChangeEvent { /** Current accuracy level of the compass */ accuracy: CompassAccuracy;}PermissionStatus
リスニングオプションの設定を構成するためのオプション。クリップボードにコピー
export interface PermissionStatus { /** * Permission state for accessing compass/location data. * On iOS, this requires location permission to access heading. * On Android, no special permissions are required for compass sensors. * * @since 7.0.0 */ compass: PermissionState;}CompassAccuracy
「CompassAccuracy」セクションコンパス精度レベル定数。
export enum CompassAccuracy { /** High accuracy - approximates to less than 5 degrees of error */ HIGH = 3, /** Medium accuracy - approximates to less than 10 degrees of error */ MEDIUM = 2, /** Low accuracy - approximates to less than 15 degrees of error */ LOW = 1, /** Unreliable accuracy - approximates to more than 15 degrees of error */ UNRELIABLE = 0, /** Unknown accuracy value */ UNKNOWN = -1,}PermissionState
「PermissionState」セクションコンパスのアクセス許可状態。
export type PermissionState = 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied';真実の源
「真実の源」セクションこのページはプラグインの src/definitions.ts API のパブリックアップストリームが変更されたときに再度同期してください。
始めから始める
「始めから始める」セクションCapgoを使用している場合 Getting Started ダッシュボードとAPIの操作を計画するには、APIに接続してください Capacitorを使用してcapgo/capacitor-compassを使用する Capacitorを使用してcapgo/capacitor-compassを使用する APIの概要 APIの実装詳細 Capgoの概要 __CAPGO_KEEP_0__の実装詳細 API Keys APIのキー __CAPGO_KEEP_0__の実装詳細 __CAPGO_KEEP_0__の実装詳細についての情報は、デバイスのページにあります。