@capacitor-community/background-geolocation

中文文档
1.3.1发布于 2026/10/22MITTypeScript零依赖近 30 天下载 37.6万

接受地理定位更新, 即使应用程序在背景中 .

中文使用文档
文档目录24 节
  1. 使用量
  2. 字体支持
  3. 安装
  4. iOS系统
  5. 机器人
  6. 更改日志
  7. v1.2.27 (中文(简体) )
  8. v1.2.26 (中文(简体) )
  9. v1.2.25 (中文(简体) )
  10. v1.2.24 (中文(简体) )
  11. v1.2.23 (中文(简体) )
  12. v1.2.22 (中文(简体) )
  13. v1.2.21 (中文(简体) )
  14. 页:1
  15. v1.2.18 (中文(简体) )
  16. v1.2.17 (中文(简体) )
  17. v1.2.16 (中文(简体) )
  18. 页:1
  19. 页:1
  20. 页:1
  21. v1.2.1 (中文(简体) )
  22. 页:1
  23. v1.0.4 (中文(简体) )
  24. v1.0.0 (韩语)

背景位置

一个 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 的支持 .

版本与发布记录

@capacitor-community/background-geolocation 共发布 50 个版本,首次发布于 2021/01/28;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.3.1latest2026/10/2218 天前
1.3.02026/10/2218 天前
1.2.262025/08/281.2 年前
1.2.252025/07/251.3 年前
1.2.242025/07/241.3 年前
1.2.232025/07/221.3 年前

关于 @capacitor-community/background-geolocation 的常见问题

@capacitor-community/background-geolocation 是什么?

接受地理定位更新, 即使应用程序在背景中 .

如何安装 @capacitor-community/background-geolocation?

在项目目录运行 npm install @capacitor-community/background-geolocation,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=3.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capacitor-community/background-geolocation 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capacitor-community/background-geolocation 可以商用吗?

@capacitor-community/background-geolocation 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@capacitor-community/background-geolocation 的下载量有多少?

根据 npm 官方下载统计,@capacitor-community/background-geolocation 在 2026/10/09 – 2026/11/07 这 30 天内被下载 375,671 次,日均约 12,522 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capacitor-community/background-geolocation 还在维护吗?

最新版本 1.3.1 发布于 2026/10/22(18 天前);至今共发布 50 个版本,其中过去 12 个月发布了 2 个;首次发布于 2021/01/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。