Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Richou/react-native-android-location-enabler
Display a GoogleMap like android popup to ask for user to enable location services if disabled
https://github.com/Richou/react-native-android-location-enabler
Last synced: 3 months ago
JSON representation
Display a GoogleMap like android popup to ask for user to enable location services if disabled
- Host: GitHub
- URL: https://github.com/Richou/react-native-android-location-enabler
- Owner: Richou
- License: mit
- Created: 2016-12-05T21:58:55.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2023-10-21T03:07:40.000Z (about 1 year ago)
- Last Synced: 2024-05-21T17:23:24.951Z (6 months ago)
- Language: Java
- Homepage:
- Size: 7.22 MB
- Stars: 194
- Watchers: 8
- Forks: 50
- Open Issues: 30
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-react-native-native-modules - react-native-android-location-enabler ★11
README
# react-native-android-location-enabler
Allow to display a GoogleMap like android popup to ask for user to enable location services if disabled
## Getting started
**ReactNative >= 0.60.0**
### Installation
`$ npm install react-native-android-location-enabler --save`
Since ReactNative 0.60.0 and ReactNative Cli 2.0.0, no action is needed to add a native module.
**ReactNative < 0.60.0**
### Installation
`$ npm install [email protected] --save`
#### Configuration
`$ react-native link react-native-android-location-enabler`
#### Manual Configuration
1. Open up `android/app/src/main/java/[...]/MainApplication.java`
- Add `import com.heanoria.library.reactnative.locationenabler.RNAndroidLocationEnablerPackage;` to the imports at the top of the file
- Add `new RNAndroidLocationEnablerPackage()` to the list returned by the `getPackages()` method2. Append the following lines to `android/settings.gradle`:
```
include ':react-native-android-location-enabler'
project(':react-native-android-location-enabler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-location-enabler/android')
```
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
```
compile project(':react-native-android-location-enabler')
```## Usage
### Check if location is enabled
```typescript
import { isLocationEnabled } from 'react-native-android-location-enabler';
import { Platform } from 'react-native';async function handleCheckPressed() {
if (Platform.OS === 'android') {
const checkEnabled: boolean = await isLocationEnabled();
console.log('checkEnabled', checkEnabled)
}
}
```### Show a modal to ask user to enable location
```typescript
import { promptForEnableLocationIfNeeded } from 'react-native-android-location-enabler';
import { Platform } from 'react-native';async function handleEnabledPressed() {
if (Platform.OS === 'android') {
try {
const enableResult = await promptForEnableLocationIfNeeded();
console.log('enableResult', enableResult);
// The user has accepted to enable the location services
// data can be :
// - "already-enabled" if the location services has been already enabled
// - "enabled" if user has clicked on OK button in the popup
} catch (error: unknown) {
if (error instanceof Error) {
console.error(error.message);
// The user has not accepted to enable the location services or something went wrong during the process
// "err" : { "code" : "ERR00|ERR01|ERR02|ERR03", "message" : "message"}
// codes :
// - ERR00 : The user has clicked on Cancel button in the popup
// - ERR01 : If the Settings change are unavailable
// - ERR02 : If the popup has failed to open
// - ERR03 : Internal error
}
}
}
}
```## Contributing
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
## License
MIT
---
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)