55package dev.icerock.moko.geo
66
77import android.content.Context
8+ import android.os.Build
89import androidx.fragment.app.FragmentManager
910import androidx.lifecycle.Lifecycle
1011import androidx.lifecycle.LifecycleObserver
@@ -36,6 +37,7 @@ actual class LocationTracker(
3637 it.priority = priority
3738 }
3839 private val locationsChannel = Channel <LatLng >(Channel .BUFFERED )
40+ private val extendedLocationsChannel = Channel <ExtendedLocation >(Channel .BUFFERED )
3941 private val trackerScope = CoroutineScope (Dispatchers .Main )
4042
4143 fun bind (lifecycle : Lifecycle , context : Context , fragmentManager : FragmentManager ) {
@@ -59,12 +61,54 @@ actual class LocationTracker(
5961 override fun onLocationResult (locationResult : LocationResult ) {
6062 super .onLocationResult(locationResult)
6163
64+ val lastLocation = locationResult.lastLocation
65+
66+ val speedAccuracy = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) null
67+ else lastLocation.speedAccuracyMetersPerSecond.toDouble()
68+
69+ val bearingAccuracy = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) null
70+ else lastLocation.bearingAccuracyDegrees.toDouble()
71+
72+ val verticalAccuracy = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) null
73+ else lastLocation.verticalAccuracyMeters.toDouble()
74+
6275 val latLng = LatLng (
63- locationResult.lastLocation.latitude,
64- locationResult.lastLocation.longitude
76+ lastLocation.latitude,
77+ lastLocation.longitude
78+ )
79+
80+ val locationPoint = Location (
81+ coordinates = latLng,
82+ coordinatesAccuracyMeters = lastLocation.accuracy.toDouble()
83+ )
84+
85+ val speed = Speed (
86+ speedMps = lastLocation.speed.toDouble(),
87+ speedAccuracyMps = speedAccuracy
88+ )
89+
90+ val azimuth = Azimuth (
91+ azimuthDegrees = lastLocation.bearing.toDouble(),
92+ azimuthAccuracyDegrees = bearingAccuracy
93+ )
94+
95+ val altitude = Altitude (
96+ altitudeMeters = lastLocation.altitude,
97+ altitudeAccuracyMeters = verticalAccuracy
98+ )
99+
100+ val extendedLocation = ExtendedLocation (
101+ location = locationPoint,
102+ azimuth = azimuth,
103+ speed = speed,
104+ altitude = altitude,
105+ timestampMs = lastLocation.time
65106 )
66107
67- trackerScope.launch { locationsChannel.send(latLng) }
108+ trackerScope.launch {
109+ extendedLocationsChannel.send(extendedLocation)
110+ locationsChannel.send(latLng)
111+ }
68112 }
69113
70114 actual suspend fun startTracking () {
@@ -93,4 +137,18 @@ actual class LocationTracker(
93137 awaitClose { job.cancel() }
94138 }
95139 }
140+
141+ actual fun getExtendedLocationsFlow (): Flow <ExtendedLocation > {
142+ return channelFlow {
143+ val sendChannel = channel
144+ val job = launch {
145+ while (isActive) {
146+ val extendedLocation = extendedLocationsChannel.receive()
147+ sendChannel.send(extendedLocation)
148+ }
149+ }
150+
151+ awaitClose { job.cancel() }
152+ }
153+ }
96154}
0 commit comments