React native geolocation service for iOS and android.
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/system level. Check the FAQ for more details.
yarn
yarn add react-native-geolocation-service
npm
npm install react-native-geolocation-service
RN Version | Package Version |
---|---|
>=0.60 | >=3.0.0 |
<0.60 | 2.0.0 |
<0.57 | 1.1.0 |
For RN 0.60 or higher, no manual linking is needed. After installing the package, just run pod install
from inside ios directory. It’ll automatically pickup the package and install it.
0.59 or below
Update your Podfile
pod 'react-native-geolocation-service', path: '../node_modules/react-native-geolocation-service'
Then run pod install
from ios directory
Open project.xcodeproj in Xcode
Drag RNFusedLocation.xcodeproj
to your project on Xcode (usually under the Libraries group on Xcode):
libRNFusedLocation.a
binary with librariesClick on your main project file (the one that represents the .xcodeproj
) select Build Phases
and drag the static library from the Products
folder inside the Library you are importing to Link Binary With Libraries
(or use the +
sign and choose library from the list):
Since the iOS implementation is written in swift, you need to add swift support in your project. It can be done just by adding an empty swift file and a bridging header in your project folder. You have to do it from xcode, otherwise swift compiler flag won’t be updated.
File -> New -> File
from xcodeNext
and say yes when prompted if you’d like to generate a bridging header (important)info.plist
There are three info.plist keys for location service
Unless you need background location update, adding only the first key will be enough. To enable background location update, you need to add all the keys in info.plist
and add location as a background mode in the Signing & Capabilities -> Capability
tab in Xcode.
For RN 0.60 or higher, no manual linking is needed. You can override following gradle properties from your root build.gradle file.
ext {
compileSdkVersion = 28
buildToolsVersion = "28.0.3"
minSdkVersion = 16
targetSdkVersion = 28
// Any of the following will work
googlePlayServicesVersion = "17.0.0"
// playServicesVersion = "17.0.0"
// playServicesLocationVersion = "17.0.0"
}
0.59 or below
In android/app/build.gradle
...
dependencies {
...
implementation 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 {
/**
+ Project-wide Gradle configuration properties
*/
ext {
compileSdkVersion = 28
minSdkVersion = 16
targetSdkVersion = 28
buildToolsVersion = "28.0.3"
googlePlayServicesVersion = "17.0.0"
}
repositories { ... }
dependencies { ... }
}
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 {
...
implementation(project(':react-native-geolocation-service')) {
exclude group: 'com.google.android.gms', module: 'play-services-location'
}
implementation 'com.google.android.gms:play-services-location:<insert your play service version here>'
}
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')
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()
);
}
}
Add permission in your AndroidManifest.xml
file based on your project requirement.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
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, for android 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() {
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 }
);
}
}
async requestAuthorization(authorizationLevel) (iOS only)
Request location permission based on the authorizationLevel parameter. Can be either "whenInUse"
or "always"
. You have to configure the plist keys during setup.
When promise resolves, returns the status of the authorization.
disabled
- Location service is disabledgranted
- Permission granteddenied
- Permission deniedrestricted
- Permission restrictedgetCurrentPosition(successCallback, ?errorCallback, ?options)
successCallback: Invoked with latest location info.
errorCallback: Invoked whenever an error is encountered.
options:
Name | Type | Default | Description |
---|---|---|---|
timeout | ms |
INFINITY |
Request timeout |
maximumAge | ms |
INFINITY |
How long previous location will be cached |
enableHighAccuracy | bool |
false |
Use high accuracy mode |
distanceFilter | m |
0 |
Minimum displacement in meters |
showLocationDialog | bool |
true |
Whether to ask to enable location in Android (android only) |
forceRequestLocation | bool |
false |
Force request location even after denying improve accuracy dialog (android only) |
watchPosition(successCallback, ?errorCallback, ?options)
successCallback: Invoked with latest location info.
errorCallback: Invoked whenever an error is encountered.
options:
Name | Type | Default | Description |
---|---|---|---|
enableHighAccuracy | bool |
false |
Use high accuracy mode |
distanceFilter | m |
100 |
Minimum displacement between location updates in meters |
interval | ms |
10000 |
Interval for active location updates (android only) |
fastestInterval | ms |
5000 |
Fastest 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) (android only) |
showLocationDialog | bool |
true |
whether to ask to enable location in Android (android only) |
forceRequestLocation | bool |
false |
Force request location even after denying improve accuracy dialog (android only) |
useSignificantChanges | bool |
false | Uses the battery-efficient native significant changes APIs to return locations. Locations will only be returned when the device detects a significant distance has been breached (iOS only) |
clearWatch(watchId)
watchPosition
)stopObserving()
Stops observing for device location changes. In addition, it removes all listeners previously registered.
Name | Code | Description |
---|---|---|
PERMISSION_DENIED | 1 | Location permission is not granted |
POSITION_UNAVAILABLE | 2 | Location provider not available |
TIMEOUT | 3 | Location request timed out |
PLAY_SERVICE_NOT_AVAILABLE | 4 | Google play service is not installed or has an older version (android only) |
SETTINGS_NOT_SATISFIED | 5 | Location service is not enabled or location mode is not appropriate for the current request (android only) |
INTERNAL_ERROR | -1 | Library crashed for some reason or the getCurrentActivity() returned null (android only) |
Author: Agontuk
Source Code: https://github.com/Agontuk/react-native-geolocation-service
#react-native #react #mobile-apps