react-native-location-settings-enabler
This package makes it easy for an React Native App to ensure that the Android device's system settings are properly configured for the app's location needs. If your app needs to request location, the device needs to enable the appropriate system settings, such as GPS or Wi-Fi scanning. Rather than directly enabling services such as the device's GPS, your app specifies the required level of accuracy/power consumption, and the device automatically makes the appropriate changes to system settings.
Installation
yarn add react-native-location-settings-enabler
Usage
Example using Hook (React Hooks API) :
const PRIORITIES: HIGH_ACCURACY useLocationSettings = LocationSettingsEnabler // React Componentconst App = { const enabled requestResolution = return <View> !enabled && <Button onPress=requestResolution title="Request Resolution Location Settings" /> </View> }
Example using Listener :
const PRIORITIES: HIGH_ACCURACY addListener checkSettings requestResolutionSettings } = LocationSettingsEnabler // Adds a listener to be invoked when location settings checked using// [checkSettings] or changed using [requestResolutionSettings]const listener = ; // Define configurationconst config = priority: HIGH_ACCURACY // default BALANCED_POWER_ACCURACY alwaysShow: true // default false needBle: false // default false; // Check if location is enabled or not; // If location is disabled, prompt the user to turn on device location; // ...// Removes this subscriptionlistener;
Example React Native App :
Clone the repo
git clone https://github.com/YsnKsy/react-native-location-settings-enabler.git && cd react-native-location-settings-enabler
Install npm dependencies
yarn example
Start Metro ( javascript bundler )
yarn example start
Install and launch example app on the device
yarn example android
API
Properties
PRIORITIES
const HIGH_ACCURACY BALANCED_POWER_ACCURACY LOW_POWER NO_POWER = LocationSettingsEnablerPRIORITIES
Static object contain a list quality of service for location updates. If your application wants high accuracy location it should set prioprity to 'HIGH_ACCURACY'. If you want negligible power impact, but to still receive location updates when available, then set priority to 'NO_POWER'.
Methods
useLocationSettings({ priority, alwaysShow, needBle }, initialStatus?)
const useLocationSettings PRIORITIES: HIGH_ACCURACY = LocationSettingsEnabler const enabled requestResolution = console // ...if !enabled
Hook let you check the user's device location status 'on' / 'off' and method let you display an activity where they can turn location 'on'.
checkSettings({ priority, alwaysShow, needBle })
const checkSettings PRIORITIES: HIGH_ACCURACY = LocationSettingsEnabler
Checking if the user's device location is turned on / off.
requestResolutionSettings({ priority, alwaysShow, needBle })
const requestResolutionSettings PRIORITIES: HIGH_ACCURACY = LocationSettingsEnabler
Display an activity where they can turn location 'on' using a location request.
addListener(callback, context?)
let listener = null { const locationEnabled = result console if listener !== null // remove listener when you finish listener } listener = LocationSettingsEnabler
Adds a listener to be invoked when onChangeLocationSettings are emitted. An optional calling context may be provided. The data arguments emitted will be passed to the listener function.
Contributing
Press the STAR
button 😀 and see the contributing guide to learn how to contribute to the repository and the development workflow.
Code of Conduct
See the code of conduct guide.
License
See the MIT License