Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 38 additions & 35 deletions src/android/plugin/google/maps/PluginLocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<Status> 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<Status>() {

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) {
Expand Down