コンテンツにスキップ
Tauri

Geolocation(位置情報)

《訳注》

Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。

デバイスの現在の位置情報を取得し(利用可能であれば、高度・方位・速度に関する情報も)追跡します。

This plugin requires a Rust version of at least 1.77.2

Platform Level Notes
windows
linux
macos
android
ios

はじめに、「geolocation 位置情報」プラグインをインストールしてください。

自分のプロジェクトのパッケージ・マネージャーを使用して依存関係を追加します:

npm run tauri add geolocation

Apple は、位置情報に関して Info.plist にプライバシー関連の記述を行なうことを義務付けています。これには、アプリが位置情報にアクセスする必要がある理由を記述する必要があります。以下に記述例を示します:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Required to do XY</string>
</dict>
</plist>

このプラグインは、AndroidManifest.xml ファイルに以下のアクセス権限を自動的に追加します:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

あなたのアプリが GPS 機能を必要とする場合は、AndroidManifest.xml ファイルに次の記述を追加する必要があります:

<uses-feature android:name="android.hardware.location.gps" android:required="true" />

Google Play ストアは、GPS 機能を有していないデバイスにこのアプリを表示するかどうかを判定するために、このプロパティ値を使用します。

「geolocation」プラグインは JavaScript で利用できます。

import {
checkPermissions,
requestPermissions,
getCurrentPosition,
watchPosition,
} from '@tauri-apps/plugin-geolocation';
let permissions = await checkPermissions();
if (
permissions.location === 'prompt' ||
permissions.location === 'prompt-with-rationale'
) {
permissions = await requestPermissions(['location']);
}
if (permissions.location === 'granted') {
const pos = await getCurrentPosition();
await watchPosition(
{ enableHighAccuracy: true, timeout: 10000, maximumAge: 0 },
(pos) => {
console.log(pos);
}
);
}

デフォルトでは、潜在的に危険なプラグイン・コマンドとそのスコープ(有効範囲)はすべてブロックされており、アクセスできません。これらを有効にするには、capabilities 設定でアクセス権限を変更する必要があります。

詳細については「セキュリティ・レベル Capabilities」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。

src-tauri/capabilities/mobile.json
{
"$schema": "../gen/schemas/mobile-schema.json",
"identifier": "mobile-capability",
"windows": ["main"],
"platforms": ["iOS", "android"],
"permissions": [
"core:default",
"geolocation:allow-check-permissions",
"geolocation:allow-request-permissions",
"geolocation:allow-get-current-position",
"geolocation:allow-watch-position"
]
}

Permission Table

Identifier Description

geolocation:allow-check-permissions

Enables the check_permissions command without any pre-configured scope.

geolocation:deny-check-permissions

Denies the check_permissions command without any pre-configured scope.

geolocation:allow-clear-permissions

Enables the clear_permissions command without any pre-configured scope.

geolocation:deny-clear-permissions

Denies the clear_permissions command without any pre-configured scope.

geolocation:allow-clear-watch

Enables the clear_watch command without any pre-configured scope.

geolocation:deny-clear-watch

Denies the clear_watch command without any pre-configured scope.

geolocation:allow-get-current-position

Enables the get_current_position command without any pre-configured scope.

geolocation:deny-get-current-position

Denies the get_current_position command without any pre-configured scope.

geolocation:allow-request-permissions

Enables the request_permissions command without any pre-configured scope.

geolocation:deny-request-permissions

Denies the request_permissions command without any pre-configured scope.

geolocation:allow-watch-position

Enables the watch_position command without any pre-configured scope.

geolocation:deny-watch-position

Denies the watch_position command without any pre-configured scope.

【※ この日本語版は、「Nov 21, 2025 英語版」に基づいています】


© 2025 Tauri Contributors. CC-BY / MIT