地理位置
获取并追踪设备的当前位置,包括海拔、航向和速度(如可用)等信息。
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 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 功能的设备展示该应用。
地理位置插件可在 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. |
© 2026 Tauri Contributors. CC-BY / MIT