Skip to content
Tauri 中文网

地理定位

获取并跟踪设备的当前位置,包括关于高度、航向和速度(如果可用)的信息。

🌐 Get and track the device’s current position, including information about altitude, heading, and speed (if available).

🌐 Supported Platforms

This plugin requires a Rust version of at least 1.77.2

Platform Level Notes
windows
linux
macos
android
ios

🌐 Setup

安装地理位置插件以开始使用。

🌐 Install the geolocation plugin to get started.

使用项目的包管理器添加依赖:

npm run tauri add geolocation

🌐 Configuration

苹果要求在 Info.plist 中为位置信息指定隐私说明,你应在其中描述你的应用为何需要访问这些信息。下面举例说明了一条示例说明:

🌐 Apple requires privacy descriptions to be specified in Info.plist for location information, where you should describe why your app needs to access it. Illustrated below is an example description:

<?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 文件中:

🌐 This plugin automatically adds the following permissions to your AndroidManifest.xml file:

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

如果你的应用需要 GPS 功能才能运行,你应当在你的 AndroidManifest.xml 文件中添加以下内容:

🌐 If your app requires GPS functionality to function, you should add the following to your AndroidManifest.xml file:

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

Google Play 商店使用此属性来决定是否应该向没有 GPS 功能的设备显示该应用。

🌐 The Google Play Store uses this property to decide whether it should show the app to devices without GPS capabilities.

🌐 Usage

地理位置插件在 JavaScript 中可用。

🌐 The geolocation plugin is available in 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);
}
);
}

🌐 Permissions

默认情况下,所有潜在危险的插件命令和作用域都会被阻止,无法访问。你必须在 capabilities 配置中修改权限以启用这些功能。

🌐 By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.

有关更多信息,请参见功能概览,有关使用插件权限的分步指南,请参见分步指南

🌐 See the Capabilities Overview for more information and the step by step guide to use plugin permissions.

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.


Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站