
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
react-native-geolocation-service
Advanced tools
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 level or in the system software 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.57+ | 2.0.0 |
<0.57 | 1.1.0 |
You need to include the NSLocationWhenInUseUsageDescription
key in Info.plist to enable geolocation when using the app. 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.
Open project.xcodeproj in Xcode
Drag RNCGeolocation.xcodeproj
to your project on Xcode (usually under the Libraries group on Xcode):
libRNCGeolocation.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):
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
targetSdkVersion = 28
buildToolsVersion = "28.0.3"
supportLibVersion = "28.0.0"
googlePlayServicesVersion = "16.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()
);
}
}
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 }
);
}
}
setRNConfiguration(options) (iOS only)
options:
Name | Type | Default | Description |
---|---|---|---|
skipPermissionRequests | bool | false | If true , you must request permissions before using Geolocation APIs. |
authorizationLevel | string | -- | Changes whether the user will be asked to give "always" or "when in use" location services permission. Any other value or auto will use the default behaviour, where the permission level is based on the contents of your Info.plist . Possible values are whenInUse , always and auto . |
requestAuthorization() (iOS only)
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(successCallback, ?errorCallback, ?options)
successCallback: Invoked with latest location info.
errorCallback: Invoked whenever an error is encountered.
options:
Name | Type | Default | Description |
---|---|---|---|
timeout | ms | -- | 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 |
forceRequestLocation | bool | false | Force request location even after denying improve accuracy dialog |
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 |
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) |
showLocationDialog | bool | true | whether to ask to enable location in Android |
forceRequestLocation | bool | false | Force request location even after denying improve accuracy dialog |
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 |
SETTINGS_NOT_SATISFIED | 5 | Location service is not enabled or location mode is not appropriate for the current request |
INTERNAL_ERROR | -1 | Library crashed for some reason or the getCurrentActivity() returned null |
Location timeout still happening ?
Try the following steps: (Taken from here)
Adjusting battery saver settings on different devices:
FAQs
React native geolocation service for iOS and android
The npm package react-native-geolocation-service receives a total of 98,751 weekly downloads. As such, react-native-geolocation-service popularity was classified as popular.
We found that react-native-geolocation-service demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.