Geolocation
The Geolocation API extends the Geolocation web spec.
As a browser polyfill, this API is available through the navigator.geolocation
global - you do not need to import
it.
Configuration and Permissions
iOS
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.
If you are using CocoaPods for React Native, make sure to include the RCTGeolocation
sub-podspec.
Android
To request access to location, you need to add the following line to your app's AndroidManifest.xml
:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Android API >= 18 Positions will also contain a mocked
boolean to indicate if position was created from a mock provider.
Android API >= 23 Requires an additional step to check for, and request the ACCESS_FINE_LOCATION permission using the PermissionsAndroid API. Failure to do so may result in a hard crash.
Methods
Reference
Methods
setRNConfiguration()
Geolocation.setRNConfiguration(config);
Sets configuration options that will be used in all location requests.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
config | object | Yes | See below. |
Supported options:
-
skipPermissionRequests
(boolean, iOS-only) - Defaults tofalse
. Iftrue
, you must request permissions before using Geolocation APIs.
requestAuthorization()
Geolocation.requestAuthorization();
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.
getCurrentPosition()
Geolocation.getCurrentPosition(geo_success, [geo_error], [geo_options]);
Invokes the success callback once with the latest location info.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
geo_success | function | Yes | Invoked with latest location info. |
geo_error | function | No | Invoked whenever an error is encountered. |
geo_options | object | No | See below. |
Supported options:
-
timeout
(ms) -
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.
watchPosition()
Geolocation.watchPosition(success, [error], [options]);
Invokes the success callback whenever the location changes. Returns a watchId
(number).
Parameters:
Name | Type | Required | Description |
---|---|---|---|
success | function | Yes | Invoked whenever the location changes. |
error | function | No | Invoked whenever an error is encountered. |
options | object | No | See below. |
Supported options:
-
timeout
(ms) -
maximumAge
(ms) - Defaults to INFINITY. -
enableHighAccuracy
(bool) -
distanceFilter
(m) -
useSignificantChanges
(bool)
clearWatch()
Geolocation.clearWatch(watchID);
Parameters:
Name | Type | Required | Description |
---|---|---|---|
watchID | number | Yes | Id as returned by watchPosition() . |
stopObserving()
Geolocation.stopObserving();
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.
© 2015–2018 Facebook Inc.
Licensed under the Creative Commons Attribution 4.0 International Public License.
https://facebook.github.io/react-native/docs/geolocation