背景位置
一个 Capacitor 插件,它允许您在应用程序背景化时获得地理定位更新 。 只支持iOS和Android平台.
使用量
import {registerPlugin} from "@capacitor/core";
const BackgroundGeolocation = registerPlugin("BackgroundGeolocation");
// To start listening for changes in the device's location, add a new watcher.
// You do this by calling 'addWatcher' with an options object and a callback. A
// Promise is returned, which resolves to the callback ID used to remove the
// watcher in the future. The callback will be called every time a new location
// is available. Watchers can not be paused, only removed. Multiple watchers may
// exist simultaneously.
BackgroundGeolocation.addWatcher(
{
// If the "backgroundMessage" option is defined, the watcher will
// provide location updates whether the app is in the background or the
// foreground. If it is not defined, location updates are only
// guaranteed in the foreground. This is true on both platforms.
// On Android, a notification must be shown to continue receiving
// location updates in the background. This option specifies the text of
// that notification.
backgroundMessage: "Cancel to prevent battery drain.",
// The title of the notification mentioned above. Defaults to "Using
// your location".
backgroundTitle: "Tracking You.",
// Whether permissions should be requested from the user automatically,
// if they are not already granted. Defaults to "true".
requestPermissions: true,
// If "true", stale locations may be delivered while the device
// obtains a GPS fix. You are responsible for checking the "time"
// property. If "false", locations are guaranteed to be up to date.
// Defaults to "false".
stale: false,
// The minimum number of metres between subsequent locations. Defaults
// to 0.
distanceFilter: 50
},
function callback(location, error) {
if (error) {
if (error.code === "NOT_AUTHORIZED") {
if (window.confirm(
"This app needs your location, " +
"but does not have permission.\n\n" +
"Open settings now?"
)) {
// It can be useful to direct the user to their device's
// settings when location permissions have been denied. The
// plugin provides the 'openSettings' method to do exactly
// this.
BackgroundGeolocation.openSettings();
}
}
return console.error(error);
}
return console.log(location);
}
).then(function after_the_watcher_has_been_added(watcher_id) {
// When a watcher is no longer needed, it should be removed by calling
// 'removeWatcher' with an object containing its ID.
BackgroundGeolocation.removeWatcher({
id: watcher_id
});
});
// The location object.
{
// Longitude in degrees.
longitude: 131.723423719132,
// Latitude in degrees.
latitude: -22.40106297456,
// Radius of horizontal uncertainty in metres, with 68% confidence.
accuracy: 11,
// Metres above sea level (or null).
altitude: 65,
// Vertical uncertainty in metres, with 68% confidence (or null).
altitudeAccuracy: 4,
// Deviation from true north in degrees (or null).
bearing: 159.60000610351562,
// True if the location was simulated by software, rather than GPS.
simulated: false,
// Speed in metres per second (or null).
speed: 23.51068878173828,
// Time the location was produced, in milliseconds since the unix epoch.
time: 1562731602000
}
// If you just want the current location, try something like this. The longer
// the timeout, the more accurate the guess will be. I wouldn't go below about
// 100ms.
function guess_location(callback, timeout) {
let last_location;
BackgroundGeolocation.addWatcher(
{
requestPermissions: false,
stale: true
},
function (location) {
last_location = location || undefined;
}
).then(function (id) {
setTimeout(function () {
callback(last_location);
Plugins.BackgroundGeolocation.removeWatcher({id});
}, timeout);
});
}
字体支持
import {BackgroundGeolocationPlugin} from "@capacitor-community/background-geolocation";
const BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>("BackgroundGeolocation");
安装
不同版本的插件支持不同版本 Capacitor :
| 电容 | 插件 |
|---|---|
| 页:1 | 页:1 |
| 页:1 | 单曲 |
| 页:1 | 单曲 |
| 五进制 | 单曲 |
| v6 数据 | 单曲 |
| ưμ㼯a | 单曲 |
读取 v0. 3 的文件 这里。 。 。 .
npm install @capacitor-community/background-geolocation
npx cap update
iOS系统
添加以下密钥到 Info.plist.数字 :
<dict>
...
<key>NSLocationWhenInUseUsageDescription</key>
<string>We need to track your location</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We need to track your location while your device is locked.</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
...
</dict>
机器人
设置 android.useLegacyBridge 选项到 true 在您的电容配置中。 这样可以防止位置更新在背景5分钟后停止. 见 https://capcorjs.com/docs/config (中文(简体) ) 财务报告和审定财务报表 https://github.com/concessor-community/background-geolocation/issues/89 互联网档案馆的存檔,存档日期2013-12-02。 。 。 .
在Android 13+上,应用程序需要 POST_NOTIFICATIONS 运行时允许显示持续通知,告知用户其位置正在背景中使用。 你也许需要 请求此权限 从用户开始,可以实现 使用 @capacitor/local-notifications 插件。 。 。 .
如果您的应用程序实时向服务器转发位置更新, 请注意在背景 Android 5分钟后, 从 WebView 启动的 HTTP 请求将会被节制 。 解决方案是使用本地的 HTTP 插件, 如 电容Http。见 https://github.com/concessor-community/后地地-地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地取地。 。 。 .
Android 的配置可以使用 strings.xml数字 :
<resources>
<!--
The channel name for the background notification. This will be visible
when the user presses & holds the notification. It defaults to
"Background Tracking".
-->
<string name="capacitor_background_geolocation_notification_channel_name">
Background Tracking
</string>
<!--
The icon to use for the background notification. Note the absence of a
leading "@". It defaults to "mipmap/ic_launcher", the app's launch icon.
If a raster image is used to generate the icon (as opposed to a vector
image), it must have a transparent background. To make sure your image
is compatible, select "Notification Icons" as the Icon Type when
creating the image asset in Android Studio.
An incompatible image asset will cause the notification to misbehave in
a few telling ways, even if the icon appears correctly:
- The notification may be dismissable by the user when it should not
be.
- Tapping the notification may open the settings, not the app.
- The notification text may be incorrect.
-->
<string name="capacitor_background_geolocation_notification_icon">
drawable/ic_tracking
</string>
<!--
The color of the notification as a string parseable by
android.graphics.Color.parseColor. Optional.
-->
<string name="capacitor_background_geolocation_notification_color">
yellow
</string>
</resources>
更改日志
v1.2.27 (中文(简体) )
- 添加 Capacitor v8 的支持 .
v1.2.26 (中文(简体) )
- 添加 Swift 软件包管理器( SPM) 的支持 .
v1.2.25 (中文(简体) )
- 添加 Capacitor v7 的支持 .
v1.2.24 (中文(简体) )
- 避免在v1.2.23中引入相撞.
v1.2.23 (中文(简体) )
- 也许让地点更新在Android上更具持久性,参见#137.
v1.2.22 (中文(简体) )
- 避免干扰Android上的安全区域设置
v1.2.21 (中文(简体) )
- 自定义Android上的通知颜色 .
页:1
- 修复一个从Android开始的防止前景服务的bug.
v1.2.18 (中文(简体) )
- 当背景监视器存在时总是显示通知,提高Android上位置更新的可靠性.
v1.2.17 (中文(简体) )
- 添加 Capacitor v6 的支持 .
v1.2.16 (中文(简体) )
- 为Android 14修复背景位置更新.
页:1
- 添加 Capacitor v5 的支持 .
页:1
- 添加 Capacitor v4 的支持 .
页:1
- 防止由于长期不活动而停止iOS上的位置更新.
v1.2.1 (中文(简体) )
- 为运行Android 12的一些设备修复背景位置更新.
页:1
- 在iOS上,状态栏现在变了蓝色,而位置则在背景中被监视. 这为用户提供了一个直接返回应用程序的方法.
v1.0.4 (中文(简体) )
- 添加数据
ACCESS_COARSE_LOCATION允许。 这对目标为Android 12(API级别为31)的应用程序来说是需要的. 一个先入为主的例子显示如何将此权限添加到您的应用程序的列表中 .
v1.0.0 (韩语)
- 抢了:
addWatcher现在返回一个承诺, 解决回调ID, 而不是回调ID本身. - 插件通过 Capacitor 导入
registerPlugin函数,而不是从Plugins对象. - IOS v11 和 Capacitor v2. 放弃支持 .
- 添加 Capacitor v3 的支持 .