Skip to content

Commit b05455d

Browse files
committed
#21 Added Altitude, Course, Speed and Accuracy
1 parent c09f415 commit b05455d

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

android/src/main/kotlin/com/icapps/background_location_tracker/flutter/FlutterBackgroundManager.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.icapps.background_location_tracker.flutter
22

33
import android.content.Context
44
import android.location.Location
5+
import android.os.Build
56
import android.os.Handler
67
import android.os.Looper
78
import com.icapps.background_location_tracker.BackgroundLocationTrackerPlugin
@@ -59,7 +60,21 @@ internal object FlutterBackgroundManager {
5960
val data = mutableMapOf<String, Any>()
6061
data["lat"] = location.latitude
6162
data["lon"] = location.longitude
63+
data["alt"] = if (location.hasAltitude()) location.altitude else 0.0
64+
data["vertical_accuracy"] = -1.0
65+
data["horizontal_accuracy"] = if (location.hasAccuracy()) location.accuracy else -1.0
66+
data["course"] = if (location.hasBearing()) location.bearing else -1.0
67+
data["course_accuracy"] = -1.0
68+
data["speed"] = if (location.hasSpeed()) location.speed else -1.0
69+
data["speed_accuracy"] = -1.0
6270
data["logging_enabled"] = SharedPrefsUtil.isLoggingEnabled(ctx)
71+
72+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
73+
data["vertical_accuracy"] = if (location.hasVerticalAccuracy()) location.verticalAccuracyMeters else -1.0
74+
data["course_accuracy"] = if (location.hasBearingAccuracy()) location.bearingAccuracyDegrees else -1.0
75+
data["speed_accuracy"] = if (location.hasSpeedAccuracy()) location.speedAccuracyMetersPerSecond else -1.0
76+
}
77+
6378
channel.invokeMethod("onLocationUpdate", data, object : MethodChannel.Result {
6479
override fun success(result: Any?) {
6580
Logger.debug("BackgroundManager", "Got success, destroy engine!")

ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,23 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate {
121121

122122
CustomLogger.log(message: "NEW LOCATION: \(location.coordinate.latitude): \(location.coordinate.longitude)")
123123

124-
let locationData: [String: Any] = [
124+
var locationData: [String: Any] = [
125125
"lat": location.coordinate.latitude,
126126
"lon": location.coordinate.longitude,
127+
"alt": location.altitude,
128+
"vertical_accuracy": location.verticalAccuracy,
129+
"horizontal_accuracy": location.horizontalAccuracy,
130+
"course": location.course,
131+
"course_accuracy": -1,
132+
"speed": location.speed,
133+
"speed_accuracy": location.speedAccuracy,
127134
"logging_enabled": SharedPrefsUtil.isLoggingEnabled(),
128135
]
129136

137+
if #available(iOS 13.4, *) {
138+
locationData["course_accuracy"] = location.courseAccuracy
139+
}
140+
130141
if SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacks {
131142
CustomLogger.log(message: "INITIALIZED, ready to send location updates")
132143
SwiftBackgroundLocationTrackerPlugin.sendLocationupdate(locationData: locationData)

lib/src/channel/background_channel.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,24 @@ class BackgroundChannel {
3636
BackgroundLocationTrackerLogger.log('locationUpdate: ${call.arguments}');
3737
final lat = data['lat'] as double; // ignore: avoid_as
3838
final lon = data['lon'] as double; // ignore: avoid_as
39-
await callback(BackgroundLocationUpdateData(lat: lat, lon: lon));
39+
final alt = data['alt'] as double; // ignore: avoid_as
40+
final verticalAccuracy = data['vertical_accuracy'] as double; // ignore: avoid_as
41+
final horizontalAccuracy = data['horizontal_accuracy'] as double; // ignore: avoid_as
42+
final course = data['course'] as double; // ignore: avoid_as
43+
final courseAccuracy = data['course_accuracy'] as double; // ignore: avoid_as
44+
final speed = data['speed'] as double; // ignore: avoid_as
45+
final speedAccuracy = data['speed_accuracy'] as double; // ignore: avoid_as
46+
await callback(BackgroundLocationUpdateData(
47+
lat: lat,
48+
lon: lon,
49+
horizontalAccuracy: horizontalAccuracy,
50+
alt: alt,
51+
verticalAccuracy: verticalAccuracy,
52+
course: course,
53+
courseAccuracy: courseAccuracy,
54+
speed: speed,
55+
speedAccuracy: speedAccuracy
56+
));
4057
return true;
4158
}
4259
}

lib/src/model/background_location_update_data.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,34 @@
22
///
33
/// latitude & longitude
44
class BackgroundLocationUpdateData {
5+
/// Latitude
56
final double lat;
7+
/// Longitude
68
final double lon;
9+
/// The radius of uncertainty for the location, measured in meters. Negative value if not available.
10+
final double horizontalAccuracy;
11+
/// Altitude
12+
final double alt;
13+
/// The validity of the altitude values, and their estimated uncertainty, measured in meters. Negative value if not available.
14+
final double verticalAccuracy;
15+
/// Direction in which the device is traveling, measured in degrees and relative to due north. Negative value if not available.
16+
final double course;
17+
/// The accuracy of the course value, measured in degrees. Negative value if not available.
18+
final double courseAccuracy;
19+
/// Instantaneous speed of the device, measured in meters per second. Negative value if not available.
20+
final double speed;
21+
/// The accuracy of the speed value, measured in meters per second. Negative value if not available.
22+
final double speedAccuracy;
723

824
const BackgroundLocationUpdateData({
925
required this.lat,
1026
required this.lon,
27+
required this.horizontalAccuracy,
28+
required this.alt,
29+
required this.verticalAccuracy,
30+
required this.course,
31+
required this.courseAccuracy,
32+
required this.speed,
33+
required this.speedAccuracy
1134
});
1235
}

0 commit comments

Comments
 (0)