Skip to content

Commit 2249a4f

Browse files
yanghao989899yanghao (DG)
andauthored
style: 按照模板修改readme文档 (#11)
Co-authored-by: yanghao (DG) <yWX1365519@china.huawei.com>
1 parent 97257d9 commit 2249a4f

File tree

1 file changed

+7
-319
lines changed

1 file changed

+7
-319
lines changed

README.md

Lines changed: 7 additions & 319 deletions
Original file line numberDiff line numberDiff line change
@@ -1,325 +1,13 @@
1-
# `@react-native-community/geolocation`
1+
# @react-native-oh-tpl/react-native-geolocation
22

3-
[![npm](https://img.shields.io/npm/v/@react-native-community/geolocation)](https://www.npmjs.com/package/@react-native-community/geolocation) ![Supports Android, iOS and web](https://img.shields.io/badge/platforms-android%20%7C%20ios%20%7C%20web-lightgrey.svg) ![MIT License](https://img.shields.io/npm/l/@react-native-community/geolocation.svg)
3+
This project is based on [@react-native-community/geolocation](https://github.com/michalchudziak/react-native-geolocation)
44

5-
The Geolocation API 📍 module for React Native that extends the [Geolocation web spec](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation).
5+
## Documentation
66

7-
Supports TurboModules ⚡️ and legacy React Native architecture.
7+
- [中文](https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/react-native-community-geolocation.md)
88

9-
Fully compatible with TypeScript.
10-
11-
Supports modern [Play Services Location API](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html).
12-
13-
14-
## Supported platforms
15-
16-
| Platform | Support |
17-
|---|---|
18-
| iOS ||
19-
| Android ||
20-
| Web ||
21-
| Windows ||
22-
| macOS ||
23-
24-
25-
26-
## Getting started
27-
28-
`yarn add @react-native-community/geolocation`
29-
30-
or
31-
32-
`npm install @react-native-community/geolocation --save`
33-
34-
## Configuration and Permissions
35-
36-
<div class="banner-crna-ejected">
37-
<h3>Projects with Native Code Only</h3>
38-
<p>
39-
This section only applies to projects made with <code>react-native init</code>
40-
or to those made with <code>expo init</code> or Create React Native App which have since ejected. For
41-
more information about ejecting, please see
42-
the <a href="https://github.com/react-community/create-react-native-app/blob/master/EJECTING.md" target="_blank">guide</a> on
43-
the Create React Native App repository.
44-
</p>
45-
</div>
46-
47-
### iOS
48-
49-
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`.
50-
51-
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.
52-
53-
IOS >= 15 Positions will also contain a `mocked` boolean to indicate if position was created from a mock provider / software.
54-
55-
56-
### Android
57-
58-
To request access to location, you need to add the following line to your app's `AndroidManifest.xml`:
59-
60-
`<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />`
61-
62-
or
63-
64-
`<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />`
65-
66-
Android API >= 18 Positions will also contain a `mocked` boolean to indicate if position was created from a mock provider.
67-
68-
<p>
69-
Android API >= 23 Requires an additional step to check for, and request
70-
the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions using
71-
the <a href="https://reactnative.dev/docs/permissionsandroid.html" target="_blank">PermissionsAndroid API</a>.
72-
Failure to do so may result in a hard crash.
73-
</p>
74-
75-
<details>
76-
<summary><b>For React Native < 0.65 on Android we need to link manually</b></summary>
77-
78-
79-
- android/settings.gradle
80-
```
81-
include ':react-native-community-geolocation'
82-
project(':react-native-community-geolocation').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/geolocation/android')
83-
```
84-
- android/app/build.gradle
85-
```
86-
dependencies {
87-
...
88-
implementation project(':react-native-community-geolocation')
89-
}
90-
```
91-
- android/app/src/main/.../MainApplication.java
92-
On imports section:
93-
```java
94-
import com.reactnativecommunity.geolocation.GeolocationPackage;
95-
```
96-
In the class at `getPackages` method:
97-
```java
98-
@Override
99-
protected List<ReactPackage> getPackages() {
100-
@SuppressWarnings("UnnecessaryLocalVariable")
101-
List<ReactPackage> packages = new PackageList(this).getPackages();
102-
// Packages that cannot be autolinked yet can be added manually here, for example:
103-
packages.add(new GeolocationPackage()); // <== add this line
104-
return packages;
105-
}
106-
```
107-
</details>
108-
109-
## Migrating from the core `react-native` module
110-
This 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:
111-
112-
```javascript
113-
navigator.geolocation.setRNConfiguration(config);
114-
```
115-
116-
to:
117-
118-
```javascript
119-
import Geolocation from '@react-native-community/geolocation';
120-
121-
Geolocation.setRNConfiguration(config);
122-
```
123-
124-
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](https://reactnative.dev/docs/platform-specific-code.html#native-specific-extensions-ie-sharing-code-with-nodejs-and-web)):
125-
126-
```javascript
127-
navigator.geolocation = require('@react-native-community/geolocation');
128-
```
129-
130-
## Usage
131-
132-
### Example
133-
134-
```javascript
135-
import Geolocation from '@react-native-community/geolocation';
136-
137-
Geolocation.getCurrentPosition(info => console.log(info));
138-
```
139-
140-
Check out the [example project](example) for more examples.
141-
142-
## Methods
143-
144-
### Summary
145-
146-
* [`setRNConfiguration`](#setrnconfiguration)
147-
* [`requestAuthorization`](#requestauthorization)
148-
* [`getCurrentPosition`](#getcurrentposition)
149-
* [`watchPosition`](#watchposition)
150-
* [`clearWatch`](#clearwatch)
151-
* [`stopObserving`](#stopobserving)
152-
153-
---
154-
155-
### Details
156-
157-
#### `setRNConfiguration()`
158-
159-
Sets configuration options that will be used in all location requests.
160-
161-
162-
```ts
163-
Geolocation.setRNConfiguration(
164-
config: {
165-
skipPermissionRequests: boolean;
166-
authorizationLevel?: 'always' | 'whenInUse' | 'auto';
167-
enableBackgroundLocationUpdates?: boolean;
168-
locationProvider?: 'playServices' | 'android' | 'auto';
169-
}
170-
) => void
171-
```
172-
173-
Supported options:
174-
175-
* `skipPermissionRequests` (boolean) - Defaults to `false`. If `true`, you must request permissions before using Geolocation APIs.
176-
* `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`.
177-
* `enableBackgroundLocationUpdates` (boolean, iOS-only) - When using `skipPermissionRequests`, toggle wether to automatically enableBackgroundLocationUpdates. Defaults to true.
178-
* `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.
179-
180-
---
181-
182-
#### `requestAuthorization()`
183-
184-
Request suitable Location permission.
185-
186-
```ts
187-
Geolocation.requestAuthorization(
188-
success?: () => void,
189-
error?: (
190-
error: {
191-
code: number;
192-
message: string;
193-
PERMISSION_DENIED: number;
194-
POSITION_UNAVAILABLE: number;
195-
TIMEOUT: number;
196-
}
197-
) => void
198-
)
199-
```
200-
201-
On iOS if NSLocationAlwaysUsageDescription is set, it will request Always authorization, although if NSLocationWhenInUseUsageDescription is set, it will request InUse authorization.
202-
203-
---
204-
205-
#### `getCurrentPosition()`
206-
207-
Invokes the success callback once with the latest location info.
208-
209-
```ts
210-
Geolocation.getCurrentPosition(
211-
success: (
212-
position: {
213-
coords: {
214-
latitude: number;
215-
longitude: number;
216-
altitude: number | null;
217-
accuracy: number;
218-
altitudeAccuracy: number | null;
219-
heading: number | null;
220-
speed: number | null;
221-
};
222-
timestamp: number;
223-
}
224-
) => void,
225-
error?: (
226-
error: {
227-
code: number;
228-
message: string;
229-
PERMISSION_DENIED: number;
230-
POSITION_UNAVAILABLE: number;
231-
TIMEOUT: number;
232-
}
233-
) => void,
234-
options?: {
235-
timeout?: number;
236-
maximumAge?: number;
237-
enableHighAccuracy?: boolean;
238-
}
239-
)
240-
```
241-
242-
243-
Supported options:
244-
245-
* `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.
246-
* `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.
247-
* `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.
248-
249-
---
250-
251-
#### `watchPosition()`
252-
253-
Invokes the success callback whenever the location changes. Returns a `watchId` (number).
254-
255-
```ts
256-
Geolocation.watchPosition(
257-
success: (
258-
position: {
259-
coords: {
260-
latitude: number;
261-
longitude: number;
262-
altitude: number | null;
263-
accuracy: number;
264-
altitudeAccuracy: number | null;
265-
heading: number | null;
266-
speed: number | null;
267-
};
268-
timestamp: number;
269-
}
270-
) => void,
271-
error?: (
272-
error: {
273-
code: number;
274-
message: string;
275-
PERMISSION_DENIED: number;
276-
POSITION_UNAVAILABLE: number;
277-
TIMEOUT: number;
278-
}
279-
) => void,
280-
options?: {
281-
interval?: number;
282-
fastestInterval?: number;
283-
timeout?: number;
284-
maximumAge?: number;
285-
enableHighAccuracy?: boolean;
286-
distanceFilter?: number;
287-
useSignificantChanges?: boolean;
288-
}
289-
) => number
290-
```
291-
292-
Supported options:
293-
294-
* `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).
295-
* `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.
296-
* `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.
297-
* `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.
298-
* `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.
299-
* `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.
300-
* `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.
301-
302-
---
303-
304-
#### `clearWatch()`
305-
306-
Clears watch observer by id returned by `watchPosition()`
307-
308-
```ts
309-
Geolocation.clearWatch(watchID: number);
310-
```
311-
312-
## Maintainers
313-
314-
This module is developed and maintained by [michalchudziak](https://github.com/michalchudziak).
315-
316-
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 ⭐️!
317-
318-
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](https://michalchudziak.dev), or send me an email at [hello@michalchudziak.dev](mailto:hello@michalchudziak.dev) 🙏🏻
319-
320-
## Contributors
321-
322-
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.
9+
- [English](https://gitee.com/react-native-oh-library/usage-docs/blob/master/en/react-native-community-geolocation.md)
32310

32411
## License
325-
The library is released under the MIT licence. For more information see `LICENSE`.
12+
13+
This library is licensed under [The MIT License (MIT)](https://github.com/michalchudziak/react-native-geolocation/blob/master/LICENSE)

0 commit comments

Comments
 (0)