搜尋套件、儲存庫與 APP 分類

套件詳情v1.6.1

background_location_tracker

一個 Flutter 插件,可讓您在 Android 與 iOS 上追蹤背景位置

AndroidiOS
近 30 天下載
830
喜歡數
76
Pub 分數
150

安裝套件

flutter pub add background_location_tracker

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 插件,可於 Android 與 iOS 上實現背景持續定位追蹤。它允許應用程式在應用程式未使用時仍能監控使用者位置,非常適合用於健身、導航或物流應用。該插件處理平台特定的設定與權限,確保跨裝置的穩定運作。它與 Flutter 生態系統無縫整合,並支援即時更新,同時對電池影響極小。

適用情境

  • 健身追蹤應用
  • 交付與物流監控
  • 地理圍欄應用
  • 旅行和導航工具
  • 基於位置的提醒

主要特色

  • 後台位置更新
  • 跨平台支援(Android 與 iOS)
  • 低電池消耗
  • 自動權限處理
  • 即時位置串流

適合對象

  • 需要持續位置追蹤的應用程式
  • 開發位置感知服務的開發者
  • 交通或健康領域的移動應用

使用建議

  • 需要使用者明確授權
  • 可能影響裝置電池壽命
  • 需要平台特定的設定
  • 隱私合規要求
  • iOS 上的後台執行限制

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

background_location_tracker

A new Flutter plugin that allows you to track the background location for Android & iOS

Build Status Coverage Status pub package

Android Config

Update compile sdk

Compile sdk should be at 29 at least.

android {
  ...
  compileSdkVersion 29
  ...

  defaultConfig {
    ...
    targetSdkVersion 29
    ...
  }
  ...
}

iOS Configuration

Update Info.plist

Add the correct permission descriptions

	<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
	<string>Your description why you should use NSLocationAlwaysAndWhenInUseUsageDescription</string>
	<key>NSLocationAlwaysUsageDescription</key>
	<string>Your description why you should use NSLocationAlwaysAndWhenInUseUsageDescription</string>
	<key>NSLocationWhenInUseUsageDescription</key>
	<string>Your description why you should use NSLocationAlwaysAndWhenInUseUsageDescription</string>

Add the background location updates in xcode

Or add the info to the Info.plist

	<key>UIBackgroundModes</key>
	<array>
		<string>location</string>
	</array>
Update the AppDelegate
import UIKit
import Flutter
import background_location_tracker

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        GeneratedPluginRegistrant.register(with: self)

        // Register plugins for background execution - this method is deprecated 
        // but kept for backward compatibility
        BackgroundLocationTrackerPlugin.setPluginRegistrantCallback { registry in
            GeneratedPluginRegistrant.register(with: registry)
        }

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

Flutter implementation

Make sure you set the @pragma('vm:entry-point') to make sure you can find the callback in release.

@pragma('vm:entry-point')
void backgroundCallback() {
  BackgroundLocationTrackerManager.handleBackgroundUpdated(
    (data) async => Repo().update(data),
  );
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await BackgroundLocationTrackerManager.initialize(
    backgroundCallback,
    config: const BackgroundLocationTrackerConfig(
      loggingEnabled: true,
      androidConfig: AndroidConfig(
        notificationIcon: 'explore',
        trackingInterval: Duration(seconds: 4),
        distanceFilterMeters: null,
      ),
      iOSConfig: IOSConfig(
        activityType: ActivityType.FITNESS,
        distanceFilterMeters: null,
        restartAfterKill: true,
      ),
    ),
  );

  runApp(MyApp());
}

Future startLocationTracking() async {
  await BackgroundLocationTrackerManager.startTracking();
}

Future stopLocationTracking() async {
  await BackgroundLocationTrackerManager.stopTracking();
}

FAQ:

I get a Unhandled Exception: MissingPluginException(No implementation found for method .... on channel ...)
This is mostly caused by a misconfiguration of the plugin:
- Android: This plugin now uses Flutter's Plugin V2 embedding pattern (FlutterPluginBinding) so you don't need to set a manual plugin registrant callback.
- iOS: For compatibility with older Flutter versions, the setPluginRegistrantCallback is still available but will be removed in a future version.

If you're using Flutter 1.12 or later, the plugin should work out of the box with the V2 embedding.

Migration Guide

Upgrading from older versions

This plugin has been updated to use the modern Flutter Plugin V2 embedding pattern with FlutterPluginBinding. If you're using Flutter 1.12 or later, you don't need to make any changes to your app's code.

For backward compatibility, the older methods are still available but marked as deprecated:

  • BackgroundLocationTrackerPlugin.setPluginRegistrantCallback on Android
  • BackgroundLocationTrackerPlugin.setPluginRegistrantCallback on iOS

These methods will be removed in a future version of the plugin.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。