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 geolocationyarn run tauri add geolocationpnpm tauri add geolocationdeno task tauri add geolocationbun tauri add geolocationcargo tauri add geolocation-
src-tauriフォルダで次のコマンドを実行して、このプラグインをCargo.toml内のプロジェクトの依存関係に追加します:cargo add tauri-plugin-geolocation --target 'cfg(any(target_os = "android", target_os = "ios"))' -
追加したプラグインを初期化するために
lib.rsを修正します:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(mobile)]app.handle().plugin(tauri_plugin_geolocation::init());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
お好みの JavaScript パッケージ・マネージャーを使用して、「JavaScript Guest」バインディングをインストールします:
npm install @tauri-apps/plugin-geolocationyarn add @tauri-apps/plugin-geolocationpnpm add @tauri-apps/plugin-geolocationdeno add npm:@tauri-apps/plugin-geolocationbun add @tauri-apps/plugin-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」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。
{ "$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 |
|---|---|
|
|
Enables the check_permissions command without any pre-configured scope. |
|
|
Denies the check_permissions command without any pre-configured scope. |
|
|
Enables the clear_permissions command without any pre-configured scope. |
|
|
Denies the clear_permissions command without any pre-configured scope. |
|
|
Enables the clear_watch command without any pre-configured scope. |
|
|
Denies the clear_watch command without any pre-configured scope. |
|
|
Enables the get_current_position command without any pre-configured scope. |
|
|
Denies the get_current_position command without any pre-configured scope. |
|
|
Enables the request_permissions command without any pre-configured scope. |
|
|
Denies the request_permissions command without any pre-configured scope. |
|
|
Enables the watch_position command without any pre-configured scope. |
|
|
Denies the watch_position command without any pre-configured scope. |
【※ この日本語版は、「Nov 21, 2025 英語版」に基づいています】
© 2025 Tauri Contributors. CC-BY / MIT