地理位置
获取并跟踪设备的当前位置,包括有关高度、航向和速度的信息(如果可用)。
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>执行 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