Get the finest location on Android using Fused API.
I created this react native module with an inspiration that none of react native's location libraries use the newer Fused API to get location. According to google, it is the most accurate way to get location in an Android device and judges by itself when to use GPS or cell towers/wifi. Thus, it works with both.
npm install react-native-fused-location --save
or
yarn add react-native-fused-location
react-native link react-native-fused-location
• in android/app/build.gradle:
dependencies {
...
compile "com.facebook.react:react-native:+" // From node_modules
+ compile project(':react-native-fused-location')
}
• in android/settings.gradle
:
...
include ':app'
+ include ':react-native-fused-location'
+ project(':react-native-fused-location').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fused-location/android')
• in MainApplication.java:
+ import com.mustansirzia.fused.FusedLocation;
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
...
+ new FusedLocation(),
...
new MainReactPackage()
);
}
Add this to your AndroidManifest.xml
:
...
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
...
<permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
android:protectionLevel="signature" />
<permission
android:name="android.permission.ACCESS_FINE_LOCATION"
android:protectionLevel="signature"/>
...
Function | Arguments | Returns | Note |
---|---|---|---|
getFusedLocation |
Nil | Location |
Call this once to get Location . Returns a promise. |
startLocationUpdates |
Nil | Nil | Call this to start receiving location updates. **Note: You still need to subscribe to fusedLocation event. So, you need to call this before you call FusedLocation.on . |
stopLocationUpdates |
Nil | Nil | Stop receiving location updates. Call this to stop listening to device's location updates. |
on |
eventName, callback |
Subscription |
Subscribe to an event. The callback with Location updates is eventName is fusedLocation . Call this after you call startLocationUpdates |
off |
Subscription |
Nil | Unsubscribe from the corresponding subscription. |
Set location accuracy. priority
be of the following types.
FusedLocation.Constants.HIGH_ACCURACY
Most accurate. Least battery efficient. Uses GPS only.
FusedLocation.Constants.BALANCED
Mixed. Chooses an appropriate provider.
FusedLocation.Constants.LOW_POWER
Least accurate. Most battery efficient. Uses Wifi/Cell Towers only.
FusedLocation.Constants.NO_POWER
Uses location updates from other apps (if they occur). Don't request location from your app.
• Default FusedLocation.Constants.BALANCED
Set an approximate interval (in milliseconds) between each location updates. Please note that this interval may not be strictly followed. Updates may come faster or slower than the interval argument.
• Default 15000
Set the minimum possible interval between location updates (in milliseconds).
• Default 10000
Set smallest amount of displacement (in meters) to occur after which the location update will be received.
• Default 0
For more info, see here.
type Location {
latitude: Number,
longitude: Number,
speed: Number,
altitude: Number,
heading: Number,
provider: String,
accuracy: Number,
bearing: Number
}
type Subscription {
listener: Function,
eventName: String
}
...
import FusedLocation from 'react-native-fused-location';
...
async componentDidMount() {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {
title: 'App needs to access your location',
message: 'App needs access to your location ' +
'so we can let our app be even more awesome.'
}
);
if (granted) {
FusedLocation.setLocationPriority(FusedLocation.Constants.HIGH_ACCURACY);
// Get location once.
const location = await FusedLocation.getFusedLocation();
this.setState({lat: location.latitude, long: location.longitude});
// Set options.
FusedLocation.setLocationPriority(FusedLocation.Constants.BALANCED);
FusedLocation.setLocationInterval(20000);
FusedLocation.setLocationFastestInterval(15000);
FusedLocation.setSmallestDisplacement(10);
// Keep getting updated location.
FusedLocation.startLocationUpdates();
// Place listeners.
this.subscription = FusedLocation.on('fusedLocation', location => {
/* location = {
latitude: 14.2323,
longitude: -2.2323,
speed: 0,
altitude: 0,
heading: 10,
provider: 'fused',
accuracy: 30,
bearing: 0
}
*/
console.log(location);
});
/* Optional
this.errSubscription = FusedLocation.on('fusedLocationError', error => {
console.warn(error);
});
*/
}
...
componentWillUnmount() {
FusedLocation.off(this.subscription);
// FusedLocation.off(this.errSubscription);
FusedLocation.stopLocationUpdates();
}
...
Tested with RN versions > 0.40.x
. For other versions I haven't had the time to test. Feel free to.
See CHANGELOG.md.
See License.