Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] location update fastest interval workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos committed Dec 18, 2018
1 parent c01c828 commit fbe7874
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.hardware.SensorManager;
import android.location.Location;
import android.os.Looper;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresPermission;
Expand Down Expand Up @@ -162,6 +163,10 @@ public final class LocationComponent {
private final CopyOnWriteArrayList<OnCameraTrackingChangedListener> onCameraTrackingChangedListeners
= new CopyOnWriteArrayList<>();

// Workaround for too frequent updates, see https://github.com/mapbox/mapbox-gl-native/issues/13587
private long fastestInterval;
private long lastUpdateTime;

/**
* Internal use.
* <p>
Expand Down Expand Up @@ -693,19 +698,21 @@ public void forceLocationUpdate(@Nullable Location location) {
@SuppressLint("MissingPermission")
public void setLocationEngine(@Nullable LocationEngine locationEngine) {
if (this.locationEngine != null) {
// If internal location engines being used, extra steps need to be taken to deconstruct the
// instance.
// If internal location engines being used, extra steps need to be taken to deconstruct the instance.
this.locationEngine.removeLocationUpdates(currentLocationEngineListener);
this.locationEngine = null;
}

if (locationEngine != null) {
fastestInterval = locationEngineRequest.getFastestInterval();
this.locationEngine = locationEngine;
if (isLayerReady && isEnabled) {
setLastLocation();
locationEngine.requestLocationUpdates(
locationEngineRequest, currentLocationEngineListener, Looper.getMainLooper());
}
} else {
fastestInterval = 0;
}
}

Expand Down Expand Up @@ -1112,6 +1119,13 @@ private void updateLocation(@Nullable final Location location, boolean fromLastL
} else if (!isLayerReady) {
lastLocation = location;
return;
} else {
long currentTime = SystemClock.elapsedRealtime();
if (currentTime - lastUpdateTime < fastestInterval) {
return;
} else {
lastUpdateTime = currentTime;
}
}

showLocationLayerIfHidden();
Expand Down

0 comments on commit fbe7874

Please sign in to comment.