react-native-enable-device-location
v1.0.12
Published
React Native module to prompt users to enable device location services (Android only)
Maintainers
Readme
📍 react-native-enable-device-location
A React Native module to prompt Android users to enable device location services (GPS) using native Google Play APIs.
✅ Supports Android only
🚫 iOS is not supported and will reject with a platform error
✨ Features
- Prompts a native Android dialog to enable location
- Graceful handling of user actions (accept or cancel)
- Promise-based API for easy integration
- Lightweight and easy to use
- Uses Google Play Services Location API
- Handles all edge cases and error scenarios
📦 Installation
# Using npm
npm install react-native-enable-device-location
# Using yarn
yarn add react-native-enable-device-locationAutolinking (React Native 0.60+)
No need to link manually. Autolinking handles everything for Android.
🛠 Manual Installation (Only for React Native < 0.60)
Android
- Update
android/settings.gradle:
include ':react-native-enable-device-location'
project(':react-native-enable-device-location').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-enable-device-location/android')- Update
android/app/build.gradle:
dependencies {
implementation 'com.google.android.gms:play-services-location:21.0.1'
implementation project(':react-native-enable-device-location')
}⚙️ Permissions
Add this to your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />📲 Usage
import { promptEnableLocation } from "react-native-enable-device-location";
import { Alert } from "react-native";
const enableLocation = async () => {
try {
const result = await promptEnableLocation();
Alert.alert("Location Enabled", result.toString());
} catch (error) {
Alert.alert("Location Error", error.message || "User cancelled or failed");
}
};🧾 API
promptEnableLocation(): Promise<boolean>
Returns
- ✅ Resolves to
trueif:- Location is already enabled
- User enables location through the prompt
Error Cases
- ❌ Rejects with an error if:
- The user cancels the prompt
- Something fails internally
- Called on an unsupported platform (like iOS)
Error Messages
"Location prompt not supported on iOS"- When called on iOS"User cancelled enabling location"- When user cancels the prompt"Location settings are inadequate"- When location settings can't be enabled"Unable to start resolution"- When the prompt can't be shown"Activity is null"- When the activity context is not available
📝 Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🤝 Support
If you find this library helpful, please give it a ⭐️ on GitHub!
