mubs_background_location 0.0.3

Flutter Android iOS

MUBS background location

# MUBS Background Location #

This plugin is currently for internal use in the MUBS project

iOS #

Update the Info.plist with the following entries

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>UIBackgroundModes</key>
<array>
	<string>location</string>
</array>

Overwrite the contents of AppDeletage.swift with the following:

import UIKit
import Flutter

import background_locator

func registerPlugins(registry: FlutterPluginRegistry) -> () {
    if (!registry.hasPlugin("BackgroundLocatorPlugin")) {
        GeneratedPluginRegistrant.register(with: registry)
    }
}


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

Usage #

  LocationManager locationManager = LocationManager.instance;
  Stream<LocationDto> dtoStream;
  StreamSubscription<LocationDto> dtoSubscription;

  @override
  void initState() {
    /// Setup Location Manager
    locationManager.distanceFilter = 5;
    locationManager.notificationTitle = 'Mobility Features';
    locationManager.notificationMsg = 'Your geo-location is being tracked';

  
    streamInit();
  }

  void streamInit() async {
    /// Subscribe to stream in case it is already running (Android only, for iOS nothing additional will happen)
    dtoStream = locationManager.dtoStream;
    dtoSubscription = dtoStream.listen(onData);

    // Subscribe if it hasn't been done already 
    if (dtoSubscription != null) dtoSubscription.cancel();

    /// Set up the subscription (again)
    dtoSubscription = dtoStream.listen(onData);
    await locationManager.start();
  }

  void onData(LocationDto dto) {
    print(dtoToString(dto));
  }

A LocationDto has the following properties:

final double latitude;
final double longitude;
final double accuracy;
final double altitude;
final double speed;
final double speedAccuracy;
final double heading;
final double time;
final bool isMocked;