React Native Geolocation complatible module that uses the new Google Play services location API on Android devices.
If this library has helped you, please support my work with a star or buy me a coffee.
This module was tested with React Native 0.57.0, but it should work smoothly with previous versions that uses Gradle 4.4 and Gradle plugin 3.1.2 or later.
* Gradle 4.4 is predefined in React Native 0.57 and uses
yarn add react-native-cross-geolocationreact-native link react-native-cross-geolocation
After that, open your android/app/build.gradle and, in the
dependencies section, change this:
Configuration and Permissions
This section only applies to projects made with
react-native init or to those made with Create React Native App which have since ejected. For more information about ejecting, please see the guide on the Create React Native App repository.
You need to include the NSLocationWhenInUseUsageDescription key in Info.plist to enable geolocation when using the app. Geolocation is enabled by default when you create a project with react-native init.
In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info.plist and add location as a background mode in the 'Capabilities' tab in Xcode.
To request access to location, you need to add the following line to your app's AndroidManifest.xml:
Android API >= 18 Positions will also contain a
mocked boolean to indicate if position was created from a mock provider.
Android API >= 23 Permissions are handled automatically.
Sets configuration options that will be used in all location requests.
skipPermissionRequests(boolean, iOS-only) - Defaults to
true, you must request permissions before using Geolocation APIs.
lowAccuracyMode(number, Android-only) - Defaults to LowAccuracyMode.BALANCED.
fastestInterval(number, Android-only) - Defaults to 10000 (10 secs).
updateInterval(number, Android-only) - Defaults to 5000 (5 secs).
Request suitable Location permission based on the key configured on pList. If NSLocationAlwaysUsageDescription is set, it will request Always authorization, although if NSLocationWhenInUseUsageDescription is set, it will request InUse authorization.
Invokes the success callback once with the latest location info.
|geo_success||function||Yes||Invoked with latest location info.|
|geo_error||function||No||Invoked whenever an error is encountered.|
timeout(ms) - Defaults to MAX_VALUE
maximumAge(ms) - Defaults to INFINITY.
enableHighAccuracy(bool) - On Android, if the location is cached this can return almost immediately, or it will request an update which might take a while.
Invokes the success callback whenever the location changes. Returns a
|success||function||Yes||Invoked whenever the location changes.|
|error||function||No||Invoked whenever an error is encountered.|
timeout(ms) - Defaults to MAX_VALUE.
maximumAge(ms) - Defaults to INFINITY.
enableHighAccuracy(bool) - Defaults to
distanceFilter(m) - Defaults to 100.
useSignificantChanges(bool) (unused in Android).
|watchID||number||Yes||Id as returned by
Stops observing for device location changes. In addition, it removes all listeners previously registered.
Notice that this method has only effect if the
geolocation.watchPosition(successCallback, errorCallback) method was previously invoked.
rnCrossGeolocation exports a
LowAccuracyMode object with values to be used in the
lowAccuracyMode property of the parameter sent to
This is the default mode.
|Request location precision to within a city block, which is an accuracy of approximately 100 meters.
This is considered a coarse level of accuracy, and is likely to consume less power. With this setting, the location services are likely to use WiFi and cell tower positioning. Note, however, that the choice of location provider depends on many other factors, such as which sources are available.
|Request city-level precision, which is an accuracy of approximately 10 Km.
This is considered a coarse level of accuracy, and is likely to consume less power.
|Use this if you need negligible impact on power consumption, but want to receive location updates when available.
With this setting, your app does not trigger any location updates, but receives locations triggered by other apps.
NOTE: These constants are only for Android, on iOS they are undefined.
Support my Work
I'm a full-stack developer with more than 20 year of experience and I try to share most of my work for free and help others, but this takes a significant amount of time and effort so, if you like my work, please consider...
Thanks for your support!