Skip to content

added showLocationDialog function #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ When promise resolves, returns the status of the authorization.
#### `stopObserving()`
Stops observing for device location changes. In addition, it removes all listeners previously registered.

#### `showLocationDialog()`
Show location enable dialog (android only).

# Error Codes
| Name | Code | Description |
| --- | --- | --- |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public void onSuccess(Location location) {
return;
}

checkLocationSettings();
checkLocationSettings(locationOptions.isShowLocationDialog());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
checkLocationSettings();
checkLocationSettings(locationOptions.isShowLocationDialog());
}
});
}
Expand Down Expand Up @@ -149,7 +149,7 @@ public void requestLocationUpdates(LocationOptions locationOptions) {
this.isSingleUpdate = false;
this.locationOptions = locationOptions;
this.locationRequest = buildLocationRequest(locationOptions);
checkLocationSettings();
checkLocationSettings(locationOptions.isShowLocationDialog());
}

@Override
Expand All @@ -169,7 +169,11 @@ private LocationRequest buildLocationRequest(LocationOptions options) {
return locationRequest;
}

private void checkLocationSettings() {
public void showLocationDialog() {
checkLocationSettings(true);
}

private void checkLocationSettings(boolean showLocationDialog) {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
Expand All @@ -192,7 +196,7 @@ public void onFailure(@NonNull Exception e) {
boolean forceRequestLocation = locationOptions.isForceRequestLocation();
boolean locationEnabled = LocationUtils.isLocationEnabled(context);

if (!showLocationDialog) {
if (!locationOptions.isShowLocationDialog()) {
if (forceRequestLocation && locationEnabled) {
startLocationUpdates();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public void removeLocationUpdates() {
locationManager.removeUpdates(locationListener);
}

@Override
public void showLocationDialog() {
/// ignore
}

@Nullable
private String getBestProvider(LocationAccuracy locationAccuracy) {
Criteria criteria = getProviderCriteria(locationAccuracy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface LocationProvider {
void requestLocationUpdates(LocationOptions locationOptions);

void removeLocationUpdates();

void showLocationDialog();
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ public void stopObserving() {
}
}

@ReactMethod
public void showLocationDialog() {
if (continuousLocationProvider != null) {
continuousLocationProvider.showLocationDialog();
}
}

@ReactMethod
public void addListener(String eventName) {
// Keep: Required for RN built in Event Emitter Calls.
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ declare module "react-native-geolocation-service" {
export function clearWatch(watchID: number): void;

export function stopObserving(): void;

export function showLocationDialog(): void;
}
4 changes: 4 additions & 0 deletions js/Geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const Geolocation = {

stopObserving: function () {
throw new Error('Method not supported by browser');
},

showLocationDialog: function () {
throw new Error('Method not supported by browser');
}
};

Expand Down
8 changes: 8 additions & 0 deletions js/Geolocation.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ const Geolocation = {

subscriptions = [];
}
},

showLocationDialog: () => {
if (Platform.OS === "ios") {
console.warn("ShowLocationDialog not supported on IOS");
} else {
RNFusedLocation.showLocationDialog();
}
}
};

Expand Down