Skip to content

Commit bb892b8

Browse files
Merge pull request #42 from virtualmarc/master
#21 Added Altitude, Course, Speed and Accuracy
2 parents c09f415 + 7eae3bf commit bb892b8

File tree

5 files changed

+112
-4
lines changed

5 files changed

+112
-4
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,42 @@
22
///
33
/// latitude & longitude
44
class BackgroundLocationUpdateData {
5+
/// Latitude
56
final double lat;
7+
8+
/// Longitude
69
final double lon;
710

11+
/// The radius of uncertainty for the location, measured in meters. Negative value if not available.
12+
final double horizontalAccuracy;
13+
14+
/// Altitude
15+
final double alt;
16+
17+
/// The validity of the altitude values, and their estimated uncertainty, measured in meters. Negative value if not available.
18+
final double verticalAccuracy;
19+
20+
/// Direction in which the device is traveling, measured in degrees and relative to due north. Negative value if not available.
21+
final double course;
22+
23+
/// The accuracy of the course value, measured in degrees. Negative value if not available.
24+
final double courseAccuracy;
25+
26+
/// Instantaneous speed of the device, measured in meters per second. Negative value if not available.
27+
final double speed;
28+
29+
/// The accuracy of the speed value, measured in meters per second. Negative value if not available.
30+
final double speedAccuracy;
31+
832
const BackgroundLocationUpdateData({
933
required this.lat,
1034
required this.lon,
35+
required this.horizontalAccuracy,
36+
required this.alt,
37+
required this.verticalAccuracy,
38+
required this.course,
39+
required this.courseAccuracy,
40+
required this.speed,
41+
required this.speedAccuracy,
1142
});
1243
}

test/src/model/background_location_update_data_test.dart

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,48 @@ import 'package:flutter_test/flutter_test.dart';
33

44
void main() {
55
test('Background location update data', () async {
6-
const data = BackgroundLocationUpdateData(lat: 51.45, lon: 4.5);
6+
const data = BackgroundLocationUpdateData(
7+
lat: 51.45,
8+
lon: 4.5,
9+
horizontalAccuracy: 1.2,
10+
alt: 42.3,
11+
verticalAccuracy: 0.3,
12+
course: 128.3,
13+
courseAccuracy: 14.3,
14+
speed: 12.2,
15+
speedAccuracy: 0.9,
16+
);
717
expect(data.lat, 51.45);
818
expect(data.lon, 4.5);
19+
expect(data.horizontalAccuracy, 1.2);
20+
expect(data.alt, 42.3);
21+
expect(data.verticalAccuracy, 0.3);
22+
expect(data.course, 128.3);
23+
expect(data.courseAccuracy, 14.3);
24+
expect(data.speed, 12.2);
25+
expect(data.speedAccuracy, 0.9);
926
});
1027

1128
test('Background location update data', () async {
12-
const data = BackgroundLocationUpdateData(lat: 51.45, lon: 4.5);
29+
const data = BackgroundLocationUpdateData(
30+
lat: 51.45,
31+
lon: 4.5,
32+
horizontalAccuracy: 1.2,
33+
alt: 42.3,
34+
verticalAccuracy: 0.3,
35+
course: 128.3,
36+
courseAccuracy: 14.3,
37+
speed: 12.2,
38+
speedAccuracy: 0.9,
39+
);
1340
expect(data.lat, 51.45);
1441
expect(data.lon, 4.5);
42+
expect(data.horizontalAccuracy, 1.2);
43+
expect(data.alt, 42.3);
44+
expect(data.verticalAccuracy, 0.3);
45+
expect(data.course, 128.3);
46+
expect(data.courseAccuracy, 14.3);
47+
expect(data.speed, 12.2);
48+
expect(data.speedAccuracy, 0.9);
1549
});
1650
}

0 commit comments

Comments
 (0)