From 4bf8f17e7bafd75b7636c96d18c1f39e6f423fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADn?= Date: Thu, 31 Oct 2019 16:28:40 +0100 Subject: [PATCH] Revert getMyLocation to way (adapting code) of 2.1.1 version Basically, change LocationServices.getFusedLocationProviderClient for LocationServices.FusedLocationApi.requestLocationUpdates --- .../google/maps/PluginLocationService.java | 73 ++++++++++--------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/src/android/plugin/google/maps/PluginLocationService.java b/src/android/plugin/google/maps/PluginLocationService.java index b385cc212..c8024ff8e 100644 --- a/src/android/plugin/google/maps/PluginLocationService.java +++ b/src/android/plugin/google/maps/PluginLocationService.java @@ -20,7 +20,11 @@ import com.google.android.gms.location.LocationResult; import com.google.android.gms.location.LocationServices; import com.google.android.gms.tasks.OnFailureListener; +import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.tasks.OnSuccessListener; +import com.google.android.gms.common.api.PendingResult; +import com.google.android.gms.common.api.Status; +import com.google.android.gms.location.LocationListener; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaInterface; @@ -456,7 +460,6 @@ private void _requestLocationUpdate(final boolean isRetry, final boolean enableH if (enableHighAccuracy || "Genymotion".equals(Build.MANUFACTURER)) { priority = LocationRequest.PRIORITY_HIGH_ACCURACY; } - if (!isRetry) { LocationServices.getFusedLocationProviderClient(cordova.getActivity()) .getLastLocation() @@ -518,46 +521,46 @@ public void onFailure(@NonNull Exception e) { .setExpirationDuration(12000) .setMaxWaitTime(5000); - LocationServices.getFusedLocationProviderClient(cordova.getActivity()).requestLocationUpdates(locationRequest, new LocationCallback() { - @Override - public void onLocationResult(LocationResult locationResult) { - Log.d(TAG, "---->onLocationResult"); - Location location = null; - if (locationResult.getLocations().size() > 0) { - location = locationResult.getLocations().get(0); - lastLocation = location; - } else if (locationResult.getLastLocation() != null) { - location = locationResult.getLastLocation(); - } + final PendingResult result = LocationServices.FusedLocationApi.requestLocationUpdates( + googleApiClient, locationRequest, new LocationListener() { - Log.d(TAG, "===> location =" + location); - if (location != null) { - JSONObject result; - Log.d(TAG, "===> location =" + callbackContext.getCallbackId()); - try { - result = PluginUtil.location2Json(location); - result.put("status", true); - callbackContext.success(result); - } catch (JSONException e) { - e.printStackTrace(); - } - } else { - // Send back the error result - JSONObject result = new JSONObject(); - try { - result.put("status", false); - result.put("error_code", "cannot_detect"); - result.put("error_message", PluginUtil.getPgmStrings(activity,"pgm_can_not_get_location")); - } catch (JSONException e) { - e.printStackTrace(); - } - callbackContext.error(result); + @Override + public void onLocationChanged(Location location) { + JSONObject result; + try { + result = PluginUtil.location2Json(location); + result.put("status", true); + callbackContext.success(result); + } catch (JSONException e) { + e.printStackTrace(); } googleApiClient.disconnect(); } - }, Looper.myLooper()); + }); + + result.setResultCallback(new ResultCallback() { + + public void onResult(Status status) { + if (!status.isSuccess()) { + String errorMsg = status.getStatusMessage(); + PluginResult result = new PluginResult(PluginResult.Status.ERROR, errorMsg); + callbackContext.sendPluginResult(result); + } else { + // Send back the error result + JSONObject result = new JSONObject(); + try { + result.put("status", false); + result.put("error_code", "cannot_detect"); + result.put("error_message", PluginUtil.getPgmStrings(activity,"pgm_can_not_get_location")); + } catch (JSONException e) { + e.printStackTrace(); + } + callbackContext.error(result); + } + } + }); } private void _onActivityResultLocationPage(Bundle bundle) {