2.0.13 • Published 5 years ago

@ammora/react-native-geolocation-service v2.0.13

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

react-native-geolocation-service

React native geolocation service for iOS and android.

Why ?

This library is created in an attempt to fix the location timeout issue on android with the react-native's current implementation of Geolocation API. This library tries to solve the issue by using Google Play Service's new FusedLocationProviderClient API, which Google strongly recommends over android's default framework location API. It automatically decides which provider to use based on your request configuration and also prompts you to change the location mode if it doesn't satisfy your current request configuration.

NOTE: Location request can still timeout since many android devices have GPS issue in the hardware level or in the system software level. Check the FAQ for more details.

Installation

yarn add react-native-geolocation-service

Compatibility

RN VersionPackage Version
0.57+2.0.0
<0.571.1.0

Setup

iOS

No additional setup is required, since it uses the React Native's default Geolocation API. Just follow the React Native documentation to modify the .plist file.

Android

  1. In android/app/build.gradle

    ...
    dependencies {
        ...
        compile project(':react-native-geolocation-service')
    }

    If you've defined project-wide properties (recommended) in your root build.gradle, this library will detect the presence of the following properties:

    buildscript {...}
    allprojects {...}
    
    /**
     + Project-wide Gradle configuration properties
     */
    ext {
        compileSdkVersion   = 25
        targetSdkVersion    = 25
        buildToolsVersion   = "25.0.2"
        supportLibVersion   = "25.0.1"
        googlePlayServicesVersion = "11.0.0"
    }

    If you do not have project-wide properties defined and have a different play-services version than the one included in this library, use the following instead. But play service version should be 11+ or the library won't work.

    ...
    dependencies {
        ...
        compile(project(':react-native-geolocation-service')) {
            exclude group: 'com.google.android.gms', module: 'play-services-location'
        }
        compile 'com.google.android.gms:play-services-location:<insert your play service version here>'
    }
  2. In android/setting.gradle

    ...
    include ':react-native-geolocation-service'
    project(':react-native-geolocation-service').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-geolocation-service/android')
  3. In MainApplication.java

    ...
    import com.agontuk.RNFusedLocation.RNFusedLocationPackage;
    
    public class MainApplication extends Application implements ReactApplication {
        ...
        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                ...
                new RNFusedLocationPackage()
            );
        }
    }

Usage

Since this library was meant to be a drop-in replacement for the RN's Geolocation API, the usage is pretty straight forward, with some extra error cases to handle.

One thing to note, this library assumes that location permission is already granted by the user, so you have to use PermissionsAndroid to request for permission before making the location request.

...
import Geolocation from 'react-native-geolocation-service';
...

componentDidMount() {
    // Instead of navigator.geolocation, just use Geolocation.
    if (hasLocationPermission) {
        Geolocation.getCurrentPosition(
            (position) => {
                console.log(position);
            },
            (error) => {
                // See error code charts below.
                console.log(error.code, error.message);
            },
            { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
        );
    }
}

API

getCurrentPosition(successCallback, ?errorCallback, ?options)

  • successCallback: Invoked with latest location info.
  • errorCallback: Invoked whenever an error is encountered.
  • options:

    NameTypeDefaultDescription
    timeoutms--Request timeout
    maximumAgemsINFINITYHow long previous location will be cached
    enableHighAccuracyboolfalseUse high accuracy mode
    distanceFilterm0Minimum displacement in meters
    showLocationDialogbooltruewhether to ask to enable location in Android

watchPosition(successCallback, ?errorCallback, ?options)

  • successCallback: Invoked with latest location info.
  • errorCallback: Invoked whenever an error is encountered.
  • options:

    NameTypeDefaultDescription
    enableHighAccuracyboolfalseUse high accuracy mode
    distanceFilterm100Minimum displacement between location updates in meters
    intervalms10000Interval for active location updates
    fastestIntervalms5000Fastest rate at which your application will receive location updates, which might be faster than interval in some situations (for example, if other applications are triggering location updates)
    showLocationDialogbooltruewhether to ask to enable location in Android

clearWatch(watchId)

  • watchId (id returned by watchPosition)

Checkout React Native documentation to see the list of available methods.

Error Codes

NameCodeDescription
PERMISSION_DENIED1Location permission is not granted
POSITION_UNAVAILABLE2Unable to determine position (not used yet)
TIMEOUT3Location request timed out
PLAY_SERVICE_NOT_AVAILABLE4Google play service is not installed or has an older version
SETTINGS_NOT_SATISFIED5Location service is not enabled or location mode is not appropriate for the current request
INTERNAL_ERROR-1Library crashed for some reason or the getCurrentActivity() returned null

TODO

  • Implement watchPosition & clearWatch methods for android
  • Implement stopObserving method for android

FAQ

  1. Location timeout still happening ?

    Try the following steps: (Taken from here)

    • Turn phone off/on
    • Turn GPS off/on
    • Disable any battery saver settings, including Power Saving Mode, Battery Management or any third party apps
    • Perform an "AGPS reset": Install the App GPS Status & Toolbox, then in that app, go to Menu > Tools > Manage A-GPS State > Reset

    Adjusting battery saver settings on different devices:

    • HTC: Access your phone settings > battery > power saving mode > battery optimization > select your app > don't optimize > save
    • Huawei: Turn Energy Settings to Normal and add your app to "Protected Apps"
    • LG If you're running Android 6 or higher: Settings > battery & power saving > battery usage > ignore optimizations > turn ON for your app
    • Motorola If you're running Android 6 or higher: Battery > select the menu in the upper right-hand corner > battery optimization > not optimized > all apps > select your app > don't optimize
    • OnePlus (using OxygenOS Settings): Battery > battery optimization > switch to 'all apps' > select your app > don't optimize
    • Samsung: Access battery settings > app power saving > details > your app > disabled
    • Sony If you're running Android 6 or higher: Battery > from the menu in the upper right-hand corner > battery optimization > apps > your app
    • Xiaomi (MIUI OS) If you're running Android 6 or higher: Access your phone settings > additional settings > battery and performance > manage battery usage > apps > your app
2.0.13

5 years ago

2.0.12

5 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago