diff --git a/android/src/main/java/com/lyokone/location/LocationPlugin.java b/android/src/main/java/com/lyokone/location/LocationPlugin.java index 5b828aa9..1c8003ef 100644 --- a/android/src/main/java/com/lyokone/location/LocationPlugin.java +++ b/android/src/main/java/com/lyokone/location/LocationPlugin.java @@ -224,34 +224,52 @@ private void getLastLocation(final Result result) { @Override public void onSuccess(Location location) { if (location != null) { - HashMap loc = new HashMap(); - loc.put("latitude", location.getLatitude()); - loc.put("longitude", location.getLongitude()); - loc.put("accuracy", (double) location.getAccuracy()); - loc.put("altitude", location.getAltitude()); - loc.put("speed", (double) location.getSpeed()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - loc.put("speed_accuracy", (double) location.getSpeedAccuracyMetersPerSecond()); - } - - if (result != null) { - result.success(loc); - return; - } - if (events != null) { - events.success(loc); - } + handleGetLastLocationResponse(location, result); } else { - if (result != null) { - result.error("ERROR", "Failed to get location.", null); - return; - } - // Do not send error on events otherwise it will produce an error + // Last location is null requestLocationUpdates required + mFusedLocationClient.requestLocationUpdates(mLocationRequest, new LocationCallback() { + @Override + public void onLocationResult(LocationResult locationResult) { + super.onLocationResult(locationResult); + + Location location = locationResult.getLastLocation(); + if (location != null) { + handleGetLastLocationResponse(location, result); + } else { + if (result != null) { + result.error("ERROR", "Failed to get location.", null); + return; + } + // Do not send error on events otherwise it will produce an error + } + mFusedLocationClient.removeLocationUpdates(this); + } + }, Looper.myLooper()); } } }); } + private void handleGetLastLocationResponse(Location location, Result result) { + HashMap loc = new HashMap(); + loc.put("latitude", location.getLatitude()); + loc.put("longitude", location.getLongitude()); + loc.put("accuracy", (double) location.getAccuracy()); + loc.put("altitude", location.getAltitude()); + loc.put("speed", (double) location.getSpeed()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + loc.put("speed_accuracy", (double) location.getSpeedAccuracyMetersPerSecond()); + } + + if (result != null) { + result.success(loc); + return; + } + if (events != null) { + events.success(loc); + } + } + @Override public void onMethodCall(MethodCall call, final Result result) { if (call.method.equals("getLocation")) {