All Projects → MustansirZia → React Native Fused Location

MustansirZia / React Native Fused Location

Licence: mit
Finest location for react-native on Android using the new Fused API.

Programming Languages

javascript
184084 projects - #8 most used programming language
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to React Native Fused Location

roam-reactnative
React Native Location SDK. High accuracy and battery efficient location SDK for iOS and Android by Roam.ai
Stars: ✭ 20 (-83.05%)
Mutual labels:  gps, location
android-amap-track-collect
这阵子由于项目需要,需要从手机上采集用户的运动轨迹数据,这样的功能大家都见到的很多了,比如咕咚、悦动圈,对跑步运动轨迹数据进行采集,再如,微信运动、钉钉运动,对于每一天你走步进行计数,如果要记录轨迹就离不开的手机定位,如果要记录步数那就离不开陀螺仪(角速度传感器),花了一天多的时间实现了一个定位数据实时采集的功能。
Stars: ✭ 50 (-57.63%)
Mutual labels:  gps, location
gps-share
Utility to share your GPS device on local network
Stars: ✭ 49 (-58.47%)
Mutual labels:  gps, location
orange3-geo
🍊 🌍 Orange add-on for dealing with geography and geo-location
Stars: ✭ 22 (-81.36%)
Mutual labels:  gps, location
P5.geolocation
a geolocation and geofencing library for p5.js
Stars: ✭ 75 (-36.44%)
Mutual labels:  gps, location
aic-mobile-ios
Art Institute of Chicago Official Mobile App
Stars: ✭ 29 (-75.42%)
Mutual labels:  gps, location
react-native-device-country
Get device location by telephony (SIM card) or settings without using GPS tracker.
Stars: ✭ 33 (-72.03%)
Mutual labels:  gps, location
LocationShare
A simple Android application to share your location
Stars: ✭ 75 (-36.44%)
Mutual labels:  gps, location
Indoorgps
Position Calculating with Trilateration via Bluetooth Beacons(Estimote)
Stars: ✭ 59 (-50%)
Mutual labels:  gps, location
Rxgps
Finding current location cannot be easier on Android !
Stars: ✭ 307 (+160.17%)
Mutual labels:  gps, location
Locokit
Location, motion, and activity recording framework for iOS
Stars: ✭ 1,353 (+1046.61%)
Mutual labels:  gps, location
Leaflet Gps
Simple leaflet control plugin for tracking gps position
Stars: ✭ 90 (-23.73%)
Mutual labels:  gps, location
surger
⚡ Is there surge pricing around me right now?
Stars: ✭ 20 (-83.05%)
Mutual labels:  gps, location
gpx-builder
Builder of GPX files
Stars: ✭ 25 (-78.81%)
Mutual labels:  gps, location
Atlas
🌎 Atlas is a set of APIs for looking up information about locations
Stars: ✭ 21 (-82.2%)
Mutual labels:  gps, location
telegram-nearby-map
Discover the location of nearby Telegram users 📡🌍
Stars: ✭ 329 (+178.81%)
Mutual labels:  gps, location
Maps
🌍🌏🌎 The whole world fits inside your cloud!
Stars: ✭ 253 (+114.41%)
Mutual labels:  gps, location
FusedBulb
Location fetch library.
Stars: ✭ 22 (-81.36%)
Mutual labels:  gps, location
EasyWayLocation
This library contain all utils related to google location. like, getting lat or long, Address and Location Setting dialog, many more...
Stars: ✭ 142 (+20.34%)
Mutual labels:  gps, location
Location
Smartphone navigation positionning, fusion GPS and IMU sensors.
Stars: ✭ 87 (-26.27%)
Mutual labels:  gps, location

react-native-fused-location

npm version npm Package Quality MIT Licence

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.

Install

npm install react-native-fused-location --save
or
yarn add react-native-fused-location

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.
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.
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.

Function Arguments Returns Note
getFusedLocation forceNewLocation Promise[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.
startLocationUpdates Nil Promise[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.
**Note: You still need to subscribe to fusedLocation event.
So, you need to call this before you call FusedLocation.on.
stopLocationUpdates Nil Promise[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).
on eventName, callback Subscription Subscribe to an event. The callback is called with Location updates if the eventName is fusedLocation.
Call this after you call startLocationUpdates
off Subscription Nil Unsubscribe from the corresponding subscription.
areProvidersAvailable Nil Promise[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)

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

setLocationInterval(interval)

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

setFastestLocationInterval(interval)

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

setSmallestDisplacement(displacement)

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

For more info, see here.

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();

}  

...


Compatibility.

• For versions < 1.0.0, use with RN versions > 0.40.x < 0.59.x.
• 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 CHANGELOG.md.

License.

See License.

NPM

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].