Skip to content

Commit

Permalink
LocationCallback added. stopLocationUpdates result check added.
Browse files Browse the repository at this point in the history
  • Loading branch information
MustansirZia committed Feb 28, 2018
1 parent 7465bd7 commit 8f64dbb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.provider.Settings;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
Expand All @@ -21,8 +22,14 @@
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationAvailability;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;

/**
Expand All @@ -31,7 +38,7 @@

public class FusedLocationModule extends ReactContextBaseJavaModule {

private static final String TAG = "REACT_NATIVE_FUSED_LOCATION";
private static final String TAG = "FUSED_LOCATION";
private final int PLAY_SERVICES_RESOLUTION_REQUEST = 2404;
private final String NATIVE_EVENT = "fusedLocation";
private final String NATIVE_ERROR = "fusedLocationError";
Expand Down Expand Up @@ -84,6 +91,7 @@ public void setSmallestDisplacement(int mSmallestDisplacement) {
this.mSmallestDisplacement = mSmallestDisplacement;
}

@SuppressWarnings("All")
@ReactMethod
public void getFusedLocation(boolean forceNewLocation, final Promise promise) {
try {
Expand All @@ -106,31 +114,40 @@ public void getFusedLocation(boolean forceNewLocation, final Promise promise) {
.build();
googleApiClient.blockingConnect();
final Location location;
if(!forceNewLocation) {
if (!forceNewLocation) {
location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
}
else {
} else {
location = null;
}
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request, new LocationListener() {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
promise.resolve(convertLocationToJSON(locationResult.getLastLocation()));
}

@Override
public void onLocationChanged(Location l) {
public void onLocationAvailability(LocationAvailability locationAvailability) {
super.onLocationAvailability(locationAvailability);
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
googleApiClient.disconnect();
promise.resolve(convertLocationToJSON(l));
if (!locationAvailability.isLocationAvailable()) {
promise.reject(TAG, "Location not available. Does your phone have GPS turned on and internet connectivity?");
}
}
});
} else {
promise.resolve(convertLocationToJSON(location));
googleApiClient.disconnect();
}, null);
return;
}
promise.resolve(convertLocationToJSON(location));
googleApiClient.disconnect();
} catch (Exception ex) {
Log.e(TAG, "Native Location Module ERR - " + ex.toString());
promise.reject(TAG, ex.toString());
}
}

@SuppressWarnings("All")
@ReactMethod
public void startLocationUpdates() {
try {
Expand Down Expand Up @@ -176,17 +193,26 @@ public void onLocationChanged(Location l) {
@ReactMethod
public void stopLocationUpdates() {
if (mGoogleApiClient != null && mLocationListener != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mLocationListener);
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mLocationListener);
pendingResult.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (!status.isSuccess()) {
Log.e(TAG, "Could not remove location updates.");
}
mGoogleApiClient.disconnect();
}
});
}
}

@ReactMethod
public boolean areProvidersAvailable() {
LocationManager lm = (LocationManager)getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
LocationManager lm = (LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
} catch (Exception ex) {
Log.e(TAG, ex.toString());
}
return gps_enabled;
Expand Down Expand Up @@ -218,7 +244,7 @@ private WritableMap convertLocationToJSON(Location l) {
params.putString("provider", l.getProvider());
params.putDouble("speed", l.getSpeed());
params.putString("timestamp", Long.toString(l.getTime()));
boolean isMock = false;
boolean isMock;
if (android.os.Build.VERSION.SDK_INT >= 18) {
isMock = l.isFromMockProvider();
} else {
Expand Down Expand Up @@ -246,7 +272,7 @@ private void sendEvent(ReactContext reactContext, String eventName, @Nullable Wr
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
} else {
Log.i(TAG, "Waiting for CatalystInstance...");
Log.i(TAG, "Waiting for Catalyst Instance...");
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-fused-location",
"version": "0.1.0",
"version": "0.2.0",
"description": "A react native module for android which gets the finest location from fused API. Gets location with or without GPS.",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 8f64dbb

Please sign in to comment.