Gathering detailed insights and metrics for @react-native-community/geolocation
Gathering detailed insights and metrics for @react-native-community/geolocation
Gathering detailed insights and metrics for @react-native-community/geolocation
Gathering detailed insights and metrics for @react-native-community/geolocation
react-native-geolocation-service
React native geolocation service for iOS and android
@react-native-community/cli-config
This package is part of the [React Native CLI](../../README.md). It contains commands for managing the configuration of React Native app.
@react-native-community/cli-doctor
This package is part of the [React Native CLI](../../README.md). It contains commands for diagnosing and fixing common Node.js, iOS, Android & React Native issues.
@react-native-community/cli
React Native CLI
Geolocation APIs for React Native
npm install @react-native-community/geolocation
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,337 Stars
261 Commits
237 Forks
22 Watching
34 Branches
104 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
Java (38.06%)
TypeScript (36.26%)
Objective-C++ (18.2%)
JavaScript (2.65%)
Kotlin (2.34%)
Ruby (1.94%)
Objective-C (0.54%)
Cumulative downloads
Total Downloads
Last day
8.3%
28,699
Compared to previous day
Last week
-1%
126,377
Compared to previous week
Last month
8%
532,900
Compared to previous month
Last year
45.4%
5,620,349
Compared to previous year
2
29
@react-native-community/geolocation
The Geolocation API 📍 module for React Native that extends the Geolocation web spec.
Supports TurboModules ⚡️ and legacy React Native architecture.
Fully compatible with TypeScript.
Supports modern Play Services Location API.
Platform | Support |
---|---|
iOS | ✅ |
Android | ✅ |
Web | ✅ |
Windows | ❌ |
macOS | ❌ |
React Native | RNC Geoloaction |
---|---|
>= 0.73.0 | >= 3.2.0 |
>= 0.70.0 | >= 3.0.0 < 3.2.0 |
>= 0.64.0 | 2.x.x |
<= 0.63.0 | 1.x.x |
yarn add @react-native-community/geolocation
or
npm install @react-native-community/geolocation --save
This section only applies to projects made with react-native init
or to those made with expo init
or 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 NSLocationWhenInUseUsageDescription
and NSLocationAlwaysAndWhenInUseUsageDescription
in Info.plist
to enable geolocation when using the app. If your app supports iOS 10 and earlier, the NSLocationAlwaysUsageDescription
key is also required. If these keys are not present in the Info.plist
, authorization requests fail immediately and silently. 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.
IOS >= 15 Positions will also contain a mocked
boolean to indicate if position was created from a mock provider / software.
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" />
or
<uses-permission android:name="android.permission.ACCESS_COARSE_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 or ACCESS_COARSE_LOCATION permissions using the PermissionsAndroid API. Failure to do so may result in a hard crash.
include ':react-native-community-geolocation'
project(':react-native-community-geolocation').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/geolocation/android')
dependencies {
...
implementation project(':react-native-community-geolocation')
}
1import com.reactnativecommunity.geolocation.GeolocationPackage;
In the class at getPackages
method:
1@Override 2protected List<ReactPackage> getPackages() { 3 @SuppressWarnings("UnnecessaryLocalVariable") 4 List<ReactPackage> packages = new PackageList(this).getPackages(); 5 // Packages that cannot be autolinked yet can be added manually here, for example: 6 packages.add(new GeolocationPackage()); // <== add this line 7 return packages; 8}
react-native
moduleThis module was created when the Geolocation was split out from the core of React Native. As a browser polyfill, this API was available through the navigator.geolocation
global - you didn't need to import it. To migrate to this module you need to follow the installation instructions above and change following code:
1navigator.geolocation.setRNConfiguration(config);
to:
1import Geolocation from '@react-native-community/geolocation'; 2 3Geolocation.setRNConfiguration(config);
If you need to have geolocation API aligned with the browser (cross-platform apps), or want to support backward compatibility, please consider adding following lines at the root level, for example at the top of your App.js file (only for react native):
1navigator.geolocation = require('@react-native-community/geolocation');
1import Geolocation from '@react-native-community/geolocation'; 2 3Geolocation.getCurrentPosition(info => console.log(info));
Check out the example project for more examples.
setRNConfiguration()
Sets configuration options that will be used in all location requests.
1Geolocation.setRNConfiguration(
2 config: {
3 skipPermissionRequests: boolean;
4 authorizationLevel?: 'always' | 'whenInUse' | 'auto';
5 enableBackgroundLocationUpdates?: boolean;
6 locationProvider?: 'playServices' | 'android' | 'auto';
7 }
8) => void
Supported options:
skipPermissionRequests
(boolean) - Defaults to false
. If true
, you must request permissions before using Geolocation APIs.authorizationLevel
(string, iOS-only) - Either "whenInUse"
, "always"
, or "auto"
. 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
.enableBackgroundLocationUpdates
(boolean, iOS-only) - When using skipPermissionRequests
, toggle wether to automatically enableBackgroundLocationUpdates. Defaults to true.locationProvider
(string, Android-only) - Either "playServices"
, "android"
, or "auto"
. Determines wether to use Google’s Location Services API
or Android’s Location API
. The "auto"
mode defaults to android
, and falls back to Android's Location API if play services aren't available.requestAuthorization()
Request suitable Location permission.
1 Geolocation.requestAuthorization( 2 success?: () => void, 3 error?: ( 4 error: { 5 code: number; 6 message: string; 7 PERMISSION_DENIED: number; 8 POSITION_UNAVAILABLE: number; 9 TIMEOUT: number; 10 } 11 ) => void 12 )
On iOS if NSLocationAlwaysUsageDescription is set, it will request Always authorization, although if NSLocationWhenInUseUsageDescription is set, it will request InUse authorization.
getCurrentPosition()
Invokes the success callback once with the latest location info.
1 Geolocation.getCurrentPosition( 2 success: ( 3 position: { 4 coords: { 5 latitude: number; 6 longitude: number; 7 altitude: number | null; 8 accuracy: number; 9 altitudeAccuracy: number | null; 10 heading: number | null; 11 speed: number | null; 12 }; 13 timestamp: number; 14 } 15 ) => void, 16 error?: ( 17 error: { 18 code: number; 19 message: string; 20 PERMISSION_DENIED: number; 21 POSITION_UNAVAILABLE: number; 22 TIMEOUT: number; 23 } 24 ) => void, 25 options?: { 26 timeout?: number; 27 maximumAge?: number; 28 enableHighAccuracy?: boolean; 29 } 30 )
Supported options:
timeout
(ms) - Is a positive value representing the maximum length of time (in milliseconds) the device is allowed to take in order to return a position. Defaults to 10 minutes.maximumAge
(ms) - Is a positive value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position. If set to Infinity the device will always return a cached position regardless of its age. Defaults to INFINITY.enableHighAccuracy
(bool) - Is a boolean representing if to use GPS or not. If set to true, a GPS position will be requested. If set to false, a WIFI location will be requested.watchPosition()
Invokes the success callback whenever the location changes. Returns a watchId
(number).
1 Geolocation.watchPosition( 2 success: ( 3 position: { 4 coords: { 5 latitude: number; 6 longitude: number; 7 altitude: number | null; 8 accuracy: number; 9 altitudeAccuracy: number | null; 10 heading: number | null; 11 speed: number | null; 12 }; 13 timestamp: number; 14 } 15 ) => void, 16 error?: ( 17 error: { 18 code: number; 19 message: string; 20 PERMISSION_DENIED: number; 21 POSITION_UNAVAILABLE: number; 22 TIMEOUT: number; 23 } 24 ) => void, 25 options?: { 26 interval?: number; 27 fastestInterval?: number; 28 timeout?: number; 29 maximumAge?: number; 30 enableHighAccuracy?: boolean; 31 distanceFilter?: number; 32 useSignificantChanges?: boolean; 33 } 34 ) => number
Supported options:
interval
(ms) -- (Android only) The rate in milliseconds at which your app prefers to receive location updates. Note that the location updates may be somewhat faster or slower than this rate to optimize for battery usage, or there may be no updates at all (if the device has no connectivity, for example).fastestInterval
(ms) -- (Android only) The fastest rate in milliseconds at which your app can handle location updates. Unless your app benefits from receiving updates more quickly than the rate specified in interval
, you don't need to set it.timeout
(ms) - Is a positive value representing the maximum length of time (in milliseconds) the device is allowed to take in order to return a position. Defaults to 10 minutes.maximumAge
(ms) - Is a positive value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position. If set to Infinity the device will always return a cached position regardless of its age. Defaults to INFINITY.enableHighAccuracy
(bool) - Is a boolean representing if to use GPS or not. If set to true, a GPS position will be requested. If set to false, a WIFI location will be requested.distanceFilter
(m) - The minimum distance from the previous location to exceed before returning a new location. Set to 0 to not filter locations. Defaults to 100m.useSignificantChanges
(bool) - 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. Defaults to FALSE.clearWatch()
Clears watch observer by id returned by watchPosition()
1Geolocation.clearWatch(watchID: number);
This module is developed and maintained by michalchudziak.
I owe a lot to the fantastic React & React Native community, and I contribute back with my free time 👨🏼💼💻 so if you like the project, please star it ⭐️!
If you need any help with this module, or anything else, feel free to reach out to me! I provide boutique consultancy services for React & React Native. Just visit my website, or send me an email at hello@michalchudziak.dev 🙏🏻
Due to personal commitments, recently I am unable to dedicate the necessary time to maintain this library as it deserves. I’m looking for passionate contributors to help keep the project alive and thriving. If you're interested in contributing or taking on a maintainer role, please reach out hello@michalchudziak.dev — your support would mean a lot!
This module was extracted from react-native
core. Please refer to https://github.com/react-native-community/react-native-geolocation/graphs/contributors for the complete list of contributors.
The library is released under the MIT licence. For more information see LICENSE
.
No vulnerabilities found.
No security vulnerabilities found.