コンテンツにジャンプ

始めて

GitHub

CapgoのAIアシストセットアップを使用してプラグインをインストールできます。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セットアップを使用する場合は、以下のコマンドを実行してプラグインをインストールし、以下のプラットフォーム固有の指示に従ってください。

ターミナル画面
bun add @capgo/capacitor-compass
bunx cap sync
import { CapgoCompass } from '@capgo/capacitor-compass';

Get the current compass heading in degrees. On iOS, the heading is updated in the background, and the latest value is returned. On Android, the heading is calculated when the method is called using accelerometer and magnetometer sensors. Not implemented on Web.

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

Start listening for compass heading changes via events. This starts the compass sensors and emits ‘headingChange’ events.

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

__CAPGO_KEEP_0__を停止する。 この機能はコンパスセンサを停止し、イベントの発行を停止します。

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

iOSでは位置情報の許可状況をチェックし、Androidでは常に「許可済み」として返します。 コンパスデータへのアクセス許可の現在の状態を確認します。

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では磁気センサの精度を監視し、精度の変更イベントを発行します。 iOSとWebではこのメソッドは何も行いません。 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();

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

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

__CAPGO_KEEP_1__

__CAPGO_KEEP_2__

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

AccuracyChangeEvent

__CAPGO_KEEP_1__

__CAPGO_KEEP_3__

export interface AccuracyChangeEvent {
/** Current accuracy level of the compass */
accuracy: CompassAccuracy;
}

PermissionStatus

__CAPGO_KEEP_4__

__CAPGO_KEEP_5__

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

__CAPGO_KEEP_6__

__CAPGO_KEEP_7__

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

コンパスのアクセス用の許可状態です。

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

このページはプラグインから生成されています。 src/definitions.ts. upstreamのパブリック API が変更されたときに、再度同期を実行してください。

あなたが使用している場合 Getting Started ダッシュボードとAPIの作業を計画し、接続するには @capgo/capacitor-compassを使用 @capgo/capacitor-compassを使用して、ネイティブ機能 APIの概要 APIの概要の実装詳細 導入 導入の実装詳細 APIのキー APIのキーとデバイスの実装詳細 デバイス デバイスの実装詳細