diff --git a/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/Geolocation.java b/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/Geolocation.java index 1506c1eb8..498a4fc8c 100644 --- a/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/Geolocation.java +++ b/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/Geolocation.java @@ -3,6 +3,7 @@ import android.content.Context; import android.location.Location; import android.location.LocationManager; +import android.os.SystemClock; import androidx.core.location.LocationManagerCompat; import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationCallback; @@ -84,4 +85,24 @@ public void clearLocationUpdates() { locationCallback = null; } } + + @SuppressWarnings("MissingPermission") + public Location getLastLocation(int maximumAge) { + Location lastLoc = null; + LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); + for (String provider : lm.getAllProviders()) { + Location tmpLoc = lm.getLastKnownLocation(provider); + if (tmpLoc != null) { + long locationAge = SystemClock.elapsedRealtimeNanos() - tmpLoc.getElapsedRealtimeNanos(); + long maximumAgeNanoSec = maximumAge * 1000000L; + if ( + locationAge <= maximumAgeNanoSec && + (lastLoc == null || lastLoc.getElapsedRealtimeNanos() > tmpLoc.getElapsedRealtimeNanos()) + ) { + lastLoc = tmpLoc; + } + } + } + return lastLoc; + } } diff --git a/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/GeolocationPlugin.java b/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/GeolocationPlugin.java index ca9667601..766de8989 100644 --- a/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/GeolocationPlugin.java +++ b/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/GeolocationPlugin.java @@ -111,23 +111,28 @@ private void completeWatchPosition(PluginCall call) { private void getPosition(PluginCall call) { boolean enableHighAccuracy = call.getBoolean("enableHighAccuracy", false); int timeout = call.getInt("timeout", 10000); + int maximumAge = call.getInt("maximumAge", 0); + Location location = implementation.getLastLocation(maximumAge); + if (location != null) { + call.resolve(getJSObjectForLocation(location)); + } else { + implementation.sendLocation( + enableHighAccuracy, + timeout, + true, + new LocationResultCallback() { + @Override + public void success(Location location) { + call.resolve(getJSObjectForLocation(location)); + } - implementation.sendLocation( - enableHighAccuracy, - timeout, - true, - new LocationResultCallback() { - @Override - public void success(Location location) { - call.resolve(getJSObjectForLocation(location)); - } - - @Override - public void error(String message) { - call.reject(message); + @Override + public void error(String message) { + call.reject(message); + } } - } - ); + ); + } } @SuppressWarnings("MissingPermission")