React Native Location Switch
A react Native module to enable location based services on Android and IOS.
Requirements
- react-native >= 0.38.0
- android buildToolsVersion 26.0.1
- gradle build tools 2.3.3
Installation Android
-
npm install react-native-location-switch
-
add the following 2 lines to your /android/settings.gradle file
include ':react-native-location-switch' project(':react-native-location-switch').projectDir = new File(settingsDir, '../node_modules/react-native-location-switch/android')
-
add the following line to your /android/app/build.gradle file
compile project(':react-native-location-switch')
-
add the "LocationSwitchPackage" import into your MainApplication.java file:
-
add the "LocationSwitchPackage" into your MainApplication.java file (getPackages method):
@Overrideprotected List<ReactPackage> {return Arrays.<ReactPackage>;} -
add the "LocationSwitch" import into your MainActivity.java file:
-
add the following code into your MainActivity.java file:
@Overridepublic void {super.;LocationSwitch..;}
Installation IOS
-
Open the project in xCode, left click on the Libraries folder -> Add files to ... and select
./node_modules/react-native-location-switch/ios/RNReactNativeLocationSwitch.xcodeproj
-
Open the project -> Build Phases -> Link Binary With Libraries and select libRNReactNativeLocationSwitch.a
React Native Interface
LocationSwitch;
LocationSwitch;
Option | Default | Info |
---|---|---|
interval | 1000 | Update interval in ms (ignored on IOS) |
requestHighAccuracy | false | If true, highest accuracy is requested. If false, "block" level accuracy is requested (ignored on IOS) |
successCallback | null | Is called when the user allows access to the location services or when the location services are already enabled |
errorCallback | null | Is called when the user denies access to the location services |
Usage
;;; const style = StyleSheet; { superprops; thisstate = locationEnabled: false ; thisonEnableLocationPress = thisonEnableLocationPress; } { LocationSwitch; } { LocationSwitch; } { if thisstatelocationEnabled return <Text style=styletextSuccess >Location enabled</Text>; return <Text style=styletext>Location disabled</Text>; } { return <View style=stylecontainer> <TouchableOpacity style=stylebutton onPress=thisonEnableLocationPress> <Text style=styletext>Enable location service</Text> </TouchableOpacity> this </View> ; } AppRegistry;