Skip to content

Commit dbd13a1

Browse files
committed
fix(android): prevent same location update
1 parent 873c034 commit dbd13a1

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

android/src/main/java/com/rngooglemapsplus/LocationHandler.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ class LocationHandler(
130130
}
131131
}
132132

133+
private fun isNewerLocation(location: Location): Boolean {
134+
val prev = lastLocation ?: return true
135+
return location.elapsedRealtimeNanos > prev.elapsedRealtimeNanos
136+
}
137+
138+
private fun notifyListener(location: Location) {
139+
lastLocation = location
140+
listener?.onLocationChanged(location)
141+
onUpdate?.invoke(location)
142+
}
143+
133144
@SuppressLint("MissingPermission")
134145
fun start() {
135146
if (isActive) return
@@ -147,11 +158,9 @@ class LocationHandler(
147158
fusedLocationClientProviderClient
148159
.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null)
149160
.addOnSuccessListener { location ->
150-
if (location != null) {
151-
lastLocation = location
152-
listener?.onLocationChanged(location)
153-
onUpdate?.invoke(location)
154-
}
161+
if (location == null) return@addOnSuccessListener
162+
if (!isNewerLocation(location)) return@addOnSuccessListener
163+
notifyListener(location)
155164
}.addOnFailureListener { e ->
156165
onError?.invoke(e.toLocationErrorCode(context))
157166
}
@@ -160,9 +169,8 @@ class LocationHandler(
160169
override fun onLocationResult(locationResult: LocationResult) {
161170
val location = locationResult.lastLocation
162171
if (location != null) {
163-
lastLocation = location
164-
listener?.onLocationChanged(location)
165-
onUpdate?.invoke(location)
172+
if (!isNewerLocation(location)) return
173+
notifyListener(location)
166174
} else {
167175
onError?.invoke(RNLocationErrorCode.POSITION_UNAVAILABLE)
168176
}

0 commit comments

Comments
 (0)