vk_location_sharing
A powerful Flutter package to implement location sharing with both foreground streams and background updates. It provides a unified API to handle permissions and location tracking with ease.
Features
- Foreground Location Stream: Real-time location updates using the
geolocatorpackage. - Background Location Updates: Periodic background location tracking using the
workmanagerpackage. - Configurable Accuracy: Easily control
LocationAccuracyanddistanceFilter. - Permission Handling: Built-in logic to request and verify location permissions for both foreground and background usage.
- Unified API: A single class
VkLocationSharingto manage the entire lifecycle of location sharing.
Getting Started
Add vk_location_sharing to your pubspec.yaml dependencies.
Platform Setup
iOS
Add the following keys to your Info.plist file:
NSLocationWhenInUseUsageDescription: Explain why you need location when in use.NSLocationAlwaysAndWhenInUseUsageDescription: Explain why you need location always.UIBackgroundModes: Addlocationandfetchto enable background updates.
Android
Add the following permissions to your AndroidManifest.xml:
android.permission.ACCESS_FINE_LOCATIONandroid.permission.ACCESS_COARSE_LOCATIONandroid.permission.ACCESS_BACKGROUND_LOCATION(Required for background sharing)
Usage
import 'package:vk_location_sharing/vk_location_sharing.dart';
final vkLocation = VkLocationSharing();
// 1. Initialise background services (if using background sharing)
await vkLocation.initializeBackground();
// 2. Start foreground sharing
final stream = await vkLocation.startForegroundSharing(
accuracy: LocationAccuracy.best,
distanceFilter: 10,
onLocationUpdate: (position) {
print("New position: ${position.latitude}, ${position.longitude}");
},
);
// 3. Start background sharing (periodic updates)
await vkLocation.startBackgroundSharing(
frequency: Duration(minutes: 15),
);
// 4. Stop sharing
await vkLocation.stopForegroundSharing();
await vkLocation.stopBackgroundSharing();
Background Tracking Accuracy
Note
Background updates are handled via Workmanager. The minimum frequency is 15 minutes (defined by Android/iOS). For near real-time background tracking, consider using a dedicated foreground service (not currently included in this package).
API Documentation
startForegroundSharing: Returns a stream ofPositionand handles permission requests.startBackgroundSharing: Registers a periodic task that runs even when the app is in the background.stopForegroundSharing/stopBackgroundSharing: Stops the respective tracking services.handlePermission: Programmatically check/request required permissions.getCurrentLocation: Fetches the current device location once.
Optimization
The package uses LocationSettings to optimize battery usage. By setting the distanceFilter, you can significantly reduce the number of updates emitted when the device is stationary or moving slowly.
Example
Check out the example directory for a full demonstration of the package including a UI to start/stop sharing.