ジオロケーション
高度、方角、速度(利用可能な場合)に関する情報を含む、デバイスの現在位置を取得および追跡します。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | | |
| linux | | |
| macos | | |
| android | ||
| ios |
ジオロケーションプラグインをインストールして開始します。
プロジェクトのパッケージマネージャーを使用して依存関係を追加します。
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 ゲストバインディングをインストールします。
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>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 機能のないデバイスにアプリを表示するかどうかを決定します。
ジオロケーションプラグインは 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 設定の権限を変更する必要があります。
詳細については、機能の概要 と、プラグイン権限を使用するための ステップバイステップガイド を参照してください。
{ "$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. |
© 2025 Tauri Contributors. CC-BY / MIT