Home

Awesome

react-native-fused-location

npm version npm Package Quality MIT Licence

Get the finest location on Android using <a href="https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi"> Fused </a> API. <br /> <br /> 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. <br />

Install

npm install react-native-fused-location --save <br /> or <br /> yarn add react-native-fused-location <br />

Automatic Link.

react-native link react-native-fused-location

Manual Link.

• 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.FusedLocationPackage;

    @Override
        protected List<ReactPackage> getPackages() {
          return Arrays.<ReactPackage>asList(
              ...
+             new FusedLocationPackage(),
              ...
              new MainReactPackage()
          );
        }

Migration to AndroidX. - BREAKING CHANGE in 1.0.0.

• Version 1.0.0 and above of this libary now makes use of AndroidX namespace instead of the legacy android support library namespace. If your app hasn't migrated to AndroidX yet, consider doing so or instead use an older version of this library such as 0.5.1. React Native 0.59 uses AndroidX. <br /> To enable AndroidX add these two lines in your android/gradle.properties file.

android.useAndroidX=true
android.enableJetifier=true

If this doesn't work out. Check out this official guide from Google. <br> A guide more specific to React Native would be here.

Permissions.

Add this to your AndroidManifest.xml:

    ...
    <uses-permission android:name="android.permission.ACCESS_COARSE_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"/>
    ...            

Usage.

API.

FunctionArgumentsReturnsNote
getFusedLocationforceNewLocationPromise[Location]Call this once to get Location. Pass optional boolean forceNewLocation to get new location update. Otherwise return the last known location. Returns a promise.
startLocationUpdatesNilPromise[Nil]Call this to start receiving location updates. The function returns a promise that will resolve after the bootstrap of the Fused provider is done. <br /> **<b>Note</b>: You still need to subscribe to fusedLocation event. <br /> So, you need to call this before you call FusedLocation.on.
stopLocationUpdatesNilPromise[Boolean]Stop receiving location updates. Call this to stop listening to device's location updates. The function returns a promise that will resolve to a boolean reflecting if the updates were indeed stoped or not (if they were already stopped beforehand).
oneventName, callbackSubscriptionSubscribe to an event. The callback is called with Location updates if the eventName is fusedLocation. <br /> Call this <b>after</b> you call startLocationUpdates
offSubscriptionNilUnsubscribe from the corresponding subscription.
areProvidersAvailableNilPromise[Boolean]Returns a promise that will always resolve to a boolean value. The resolved value reflects the providers' availability; true when location providers are available and false otherwise.

Configuration.

setLocationPriority(priority) <br />

Set location accuracy. priority be of the following types. <br /> <b>FusedLocation.Constants.HIGH_ACCURACY</b> Most accurate. Least battery efficient. Uses GPS only. <br /> <b>FusedLocation.Constants.BALANCED</b> Mixed. Chooses an appropriate provider. <br /> <b>FusedLocation.Constants.LOW_POWER</b> Least accurate. Most battery efficient. Uses Wifi/Cell Towers only. <br /> <b>FusedLocation.Constants.NO_POWER</b> Uses location updates from other apps (if they occur). Don't request location from your app. <br /> • Default FusedLocation.Constants.BALANCED<br />

setLocationInterval(interval) <br />

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. <br /> • Default 15000

setFastestLocationInterval(interval) <br />

Set the minimum possible interval between location updates (in milliseconds). <br /> • Default 10000

setSmallestDisplacement(displacement) <br />

Set smallest amount of displacement (in meters) to occur after which the location update will be received. <br /> • Default 0

For more info, see <a href="https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest"> here. </a>

Types.

type Location {
        latitude: Number,
        longitude: Number,
        speed: Number,
        altitude: Number,
        provider: String,
        accuracy: Number,
        bearing: Number,
        mocked: Boolean,
        timestamp: String
}
type Subscription {
        listener: Function,
        eventName: String
}

Example.

...
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.setFastestLocationInterval(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,
             provider: 'fused',
             accuracy: 30,
             bearing: 10,
             mocked: false,
             timestamp: '1513190221416'
           }
           */
           console.log(location);
        });

        /* Optional
        this.errSubscription = FusedLocation.on('fusedLocationError', error => {
            console.warn(error);
        });
        */
     }

...

componentWillUnmount() {

    FusedLocation.off(this.subscription);
    // FusedLocation.off(this.errSubscription);
    FusedLocation.stopLocationUpdates();

}  

...

<br />

Compatibility.

• For versions < 1.0.0, use with RN versions > 0.40.x < 0.59.x. <br /> • For versions >= 1.0.0, use with RN versions > 0.59.x.

Tested with Android SDK version >= 16 (Android 4.1 - Jelly Bean). Please feel free to test it with other versions.

This repository follows Semantic Versioning. No breaking changes will be incorporated till v2.x.x.

Release Notes.

See <a href="https://github.com/MustansirZia/react-native-fused-location/blob/master/CHANGELOG.md"> CHANGELOG.md</a>.

License.

See <a href="https://github.com/MustansirZia/react-native-fused-location/blob/master/LICENSE.txt"> License</a>.

Support.

Support my OSS work by buying me a coffee!

<a href="https://www.buymeacoffee.com/MustansirZia" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-blue.png" alt="Buy Me A Pizza" style="height: 60px !important;width: 217px !important;" ></a>