コンテンツにジャンプ

Getting Started

ターミナル画面
bun add @capgo/capacitor-compass
bunx cap sync

「インポート」のセクション

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

「API オーバーレイ」のセクション

Section titled “API Overview”

現在のコンパス方向を取得します。

import { CapgoCompass } from '@capgo/capacitor-compass';
const { value } = await CapgoCompass.getCurrentHeading();
console.log('Compass heading:', value, 'degrees');

コンパスヘッドの変更イベントを待ち受けるようにします。 コンパスセンサを開始し、'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);
});

コンパスヘッドの変更イベントを待ち受けるのを止めます。 コンパスセンサを停止し、イベントの発行を停止します。

import { CapgoCompass } from '@capgo/capacitor-compass';
await CapgoCompass.stopListening();

コンパスデータへのアクセス許可の現在の状態を確認します。 iOS の場合、位置許可の状態を確認します。 Android の場合、常に 'granted' を返します。許可は必要ありません。

import { CapgoCompass } from '@capgo/capacitor-compass';
const status = await CapgoCompass.checkPermissions();
console.log('Compass permission:', status.compass);

コンパスデータへのアクセス許可を要求します。 iOS の場合、ヘッドデータのために位置許可を要求します。 Android の場合、すぐに解決されます。許可は必要ありません。

import { CapgoCompass } from '@capgo/capacitor-compass';
const status = await CapgoCompass.requestPermissions();
if (status.compass === 'granted') {
// Can now use compass
}

コンパス精度の監視を開始します。 Androidでは、磁気センサの精度を監視し、精度の変更イベントを発行します。 開発者は、これらのイベントにリスナーを追加し、カリブレーション用のUIを実装できます。 iOSとWebでは、このメソッドは何も実行しません。コンパス精度の監視は利用できません。

import { CapgoCompass } from '@capgo/capacitor-compass';
// Start monitoring accuracy
await CapgoCompass.watchAccuracy();
// Listen for accuracy changes and implement custom UI
CapgoCompass.addListener('accuracyChange', (event) => {
console.log('Accuracy changed to:', event.accuracy);
if (event.accuracy < CompassAccuracy.MEDIUM) {
// Show your custom calibration UI
}
});

コンパス精度の監視を停止します。 精度の監視を停止します。

import { CapgoCompass } from '@capgo/capacitor-compass';
await CapgoCompass.unwatchAccuracy();

現在のコンパス精度を取得します。 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');
}

「型の参照」セクション

Copy to clipboard

CompassHeading

CompassHeading

コンパス向きの値

export interface CompassHeading {
/** Compass heading in degrees (0-360) */
value: number;
}

ListeningOptions

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

HeadingChangeEvent

向きの変更イベントデータ

export interface HeadingChangeEvent {
/** Compass heading in degrees (0-360) */
value: number;
}

AccuracyChangeEvent

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;
}

コピー

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

コピー

真実の源

export type PermissionState = 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied';

このページはプラグインから生成されています

__CAPGO_KEEP_0__

__CAPGO_KEEP_0__ src/definitions.ts. upstream の API が変更されたときに、再度 sync を実行してください。

Getting Started から続けてください。

Getting Started から続けてください。

あなたが使用している場合 Getting Started ダッシュボードと API の作業を計画するには、接続してください。 native capability を使用するために @capgo/capacitor-compass を使用します。 native capability を使用するために @capgo/capacitor-compass を使用します。 API の概要 API の実装詳細 __CAPGO_KEEP_0__ の実装詳細 導入 API キー API キーと デバイス デバイスの実装詳細について