react-native-nitro-geolocation 1.1.2 β 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +250 -0
- package/package.json +1 -1
- package/src/devtools/index.test.ts +54 -0
- package/src/devtools/index.ts +7 -1
package/README.md
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
# react-native-nitro-geolocation
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/react-native-nitro-geolocation)
|
|
4
|
+
|
|
5
|
+
**Simple and Modern Geolocation for React Native** β Powered by Nitro Modules with JSI
|
|
6
|
+
|
|
7
|
+
A complete reimplementation of [`@react-native-community/geolocation`](https://github.com/michalchudziak/react-native-geolocation) for the React Native New Architecture, featuring:
|
|
8
|
+
|
|
9
|
+
- π― **Simple functional API** β Direct function calls, no complex abstractions
|
|
10
|
+
- β‘ **JSI-powered performance** β Direct native calls without the Bridge
|
|
11
|
+
- π **100% API compatibility** via `/compat` for easy migration
|
|
12
|
+
- π§Ή **Automatic cleanup** β No manual subscription management
|
|
13
|
+
- π± **Consistent behavior** across iOS and Android
|
|
14
|
+
- π οΈ **DevTools Plugin** β Mock locations with interactive map (Rozenite)
|
|
15
|
+
|
|
16
|
+

|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## π Documentation
|
|
21
|
+
|
|
22
|
+
Full documentation available at:
|
|
23
|
+
π [https://react-native-nitro-geolocation.pages.dev](https://react-native-nitro-geolocation.pages.dev)
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## π§ Introduction
|
|
28
|
+
|
|
29
|
+
React Native Nitro Geolocation provides **two APIs** to fit your needs:
|
|
30
|
+
|
|
31
|
+
### 1. Modern API (Recommended)
|
|
32
|
+
|
|
33
|
+
**Simple functional API** with direct calls and a single hook for tracking:
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import {
|
|
37
|
+
setConfiguration,
|
|
38
|
+
requestPermission,
|
|
39
|
+
getCurrentPosition,
|
|
40
|
+
useWatchPosition,
|
|
41
|
+
} from "react-native-nitro-geolocation";
|
|
42
|
+
|
|
43
|
+
// Configure once at app startup
|
|
44
|
+
setConfiguration({
|
|
45
|
+
authorizationLevel: "whenInUse",
|
|
46
|
+
locationProvider: "auto",
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Request permission
|
|
50
|
+
const status = await requestPermission();
|
|
51
|
+
|
|
52
|
+
// Get current location
|
|
53
|
+
const position = await getCurrentPosition({
|
|
54
|
+
enableHighAccuracy: true,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Continuous tracking with hook
|
|
58
|
+
function LocationTracker() {
|
|
59
|
+
const { position, error, isWatching } = useWatchPosition({
|
|
60
|
+
enabled: true,
|
|
61
|
+
enableHighAccuracy: true,
|
|
62
|
+
distanceFilter: 10,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (error) return <Text>Error: {error.message}</Text>;
|
|
66
|
+
if (!position) return <Text>Waiting...</Text>;
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<Text>
|
|
70
|
+
{position.coords.latitude}, {position.coords.longitude}
|
|
71
|
+
</Text>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Benefits**:
|
|
77
|
+
|
|
78
|
+
### 2. Legacy API (Compatibility)
|
|
79
|
+
|
|
80
|
+
**Drop-in replacement** for `@react-native-community/geolocation`:
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
import Geolocation from "react-native-nitro-geolocation/compat";
|
|
84
|
+
|
|
85
|
+
Geolocation.getCurrentPosition(
|
|
86
|
+
(position) => console.log(position),
|
|
87
|
+
(error) => console.error(error),
|
|
88
|
+
{ enableHighAccuracy: true }
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const watchId = Geolocation.watchPosition((position) => console.log(position));
|
|
92
|
+
Geolocation.clearWatch(watchId);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## β‘ Quick Start
|
|
98
|
+
|
|
99
|
+
### 1. Installation
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
# Install Nitro core and Geolocation module
|
|
103
|
+
yarn add react-native-nitro-modules react-native-nitro-geolocation
|
|
104
|
+
|
|
105
|
+
# or using npm
|
|
106
|
+
npm install react-native-nitro-modules react-native-nitro-geolocation
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Rebuild your native app:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
cd ios && pod install
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
### 2. iOS Setup
|
|
118
|
+
|
|
119
|
+
Add permissions to your **Info.plist**:
|
|
120
|
+
|
|
121
|
+
```xml
|
|
122
|
+
<key>NSLocationWhenInUseUsageDescription</key>
|
|
123
|
+
<string>This app requires access to your location while it's in use.</string>
|
|
124
|
+
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
|
|
125
|
+
<string>This app requires access to your location at all times.</string>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
### 3. Android Setup
|
|
131
|
+
|
|
132
|
+
Add permissions to **AndroidManifest.xml**:
|
|
133
|
+
|
|
134
|
+
```xml
|
|
135
|
+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
|
136
|
+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Optional (for background):
|
|
140
|
+
|
|
141
|
+
```xml
|
|
142
|
+
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
### 4. Development Tools (Optional)
|
|
148
|
+
|
|
149
|
+
#### DevTools Plugin (Rozenite)
|
|
150
|
+
|
|
151
|
+
> **Prerequisites**: Requires [Rozenite DevTools](https://github.com/rozenite/rozenite) to be installed.
|
|
152
|
+
>
|
|
153
|
+
> **API Compatibility**: Only works with the Modern API. Does not support the Legacy API (`/compat`).
|
|
154
|
+
|
|
155
|
+
Mock geolocation data during development with an interactive map interface:
|
|
156
|
+
|
|
157
|
+

|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
npm install @react-native-nitro-geolocation/rozenite-plugin
|
|
161
|
+
# or
|
|
162
|
+
yarn add @react-native-nitro-geolocation/rozenite-plugin
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
**Setup**:
|
|
166
|
+
|
|
167
|
+
```tsx
|
|
168
|
+
import {
|
|
169
|
+
useGeolocationDevTools,
|
|
170
|
+
createPosition,
|
|
171
|
+
} from "@react-native-nitro-geolocation/rozenite-plugin";
|
|
172
|
+
|
|
173
|
+
function App() {
|
|
174
|
+
// Enable location mocking in development
|
|
175
|
+
useGeolocationDevTools({
|
|
176
|
+
initialPosition: createPosition("Seoul, South Korea"),
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return <YourApp />;
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**Features**:
|
|
184
|
+
|
|
185
|
+
- πΊοΈ Interactive Leaflet map interface
|
|
186
|
+
- π Click to set location instantly
|
|
187
|
+
- β¨οΈ Arrow key navigation for precise control
|
|
188
|
+
- ποΈ 20 pre-configured city presets
|
|
189
|
+
- βοΈ Manual latitude/longitude input
|
|
190
|
+
- π Real-time heading, speed, and accuracy calculation
|
|
191
|
+
- π Dark mode support
|
|
192
|
+
|
|
193
|
+
[See full DevTools guide β](https://react-native-nitro-geolocation.pages.dev/guide/devtools)
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
### 5. Usage
|
|
198
|
+
|
|
199
|
+
#### Modern API (Recommended)
|
|
200
|
+
|
|
201
|
+
```tsx
|
|
202
|
+
// Get current location
|
|
203
|
+
const position = await getCurrentPosition({ enableHighAccuracy: true });
|
|
204
|
+
|
|
205
|
+
// Real-time tracking with hook
|
|
206
|
+
const { position, error } = useWatchPosition({
|
|
207
|
+
enabled: true,
|
|
208
|
+
distanceFilter: 10
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
#### Legacy API (Compatibility)
|
|
213
|
+
|
|
214
|
+
```tsx
|
|
215
|
+
import Geolocation from "react-native-nitro-geolocation/compat";
|
|
216
|
+
|
|
217
|
+
Geolocation.getCurrentPosition((pos) => console.log(pos));
|
|
218
|
+
const watchId = Geolocation.watchPosition((pos) => console.log(pos));
|
|
219
|
+
Geolocation.clearWatch(watchId);
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
## π Migration from `@react-native-community/geolocation`
|
|
227
|
+
|
|
228
|
+
Change the import to use `/compat` β 100% API compatible:
|
|
229
|
+
```diff
|
|
230
|
+
- import Geolocation from '@react-native-community/geolocation';
|
|
231
|
+
+ import Geolocation from 'react-native-nitro-geolocation/compat';
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
## π Learn More
|
|
237
|
+
|
|
238
|
+
- [Introduction](https://react-native-nitro-geolocation.pages.dev/guide/)
|
|
239
|
+
- [Quick Start Guide](https://react-native-nitro-geolocation.pages.dev/guide/quick-start)
|
|
240
|
+
- [Modern API Reference](https://react-native-nitro-geolocation.pages.dev/guide/modern-api)
|
|
241
|
+
- [Legacy API Reference](https://react-native-nitro-geolocation.pages.dev/guide/legacy-api)
|
|
242
|
+
- [DevTools Plugin Guide](https://react-native-nitro-geolocation.pages.dev/guide/devtools)
|
|
243
|
+
- [Why Nitro Module?](https://react-native-nitro-geolocation.pages.dev/guide/why-nitro-module)
|
|
244
|
+
- [Benchmark Results](https://react-native-nitro-geolocation.pages.dev/guide/benchmark)
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
MIT License.
|
package/package.json
CHANGED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { isDevtoolsEnabled } from "./index";
|
|
3
|
+
|
|
4
|
+
const globalState = globalThis as typeof globalThis & {
|
|
5
|
+
__DEV__?: boolean;
|
|
6
|
+
__geolocationDevToolsEnabled?: boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const originalState = {
|
|
10
|
+
hasDev: Object.prototype.hasOwnProperty.call(globalState, "__DEV__"),
|
|
11
|
+
dev: globalState.__DEV__,
|
|
12
|
+
hasDevtoolsEnabled: Object.prototype.hasOwnProperty.call(
|
|
13
|
+
globalState,
|
|
14
|
+
"__geolocationDevToolsEnabled"
|
|
15
|
+
),
|
|
16
|
+
devtoolsEnabled: globalState.__geolocationDevToolsEnabled
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
describe("isDevtoolsEnabled", () => {
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
if (originalState.hasDev) {
|
|
22
|
+
globalState.__DEV__ = originalState.dev;
|
|
23
|
+
} else {
|
|
24
|
+
globalState.__DEV__ = undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (originalState.hasDevtoolsEnabled) {
|
|
28
|
+
globalState.__geolocationDevToolsEnabled = originalState.devtoolsEnabled;
|
|
29
|
+
} else {
|
|
30
|
+
globalState.__geolocationDevToolsEnabled = undefined;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("enables devtools only when React Native __DEV__ and the devtools flag are true", () => {
|
|
35
|
+
globalState.__DEV__ = true;
|
|
36
|
+
globalState.__geolocationDevToolsEnabled = true;
|
|
37
|
+
|
|
38
|
+
expect(isDevtoolsEnabled()).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("ignores the devtools flag outside React Native __DEV__", () => {
|
|
42
|
+
globalState.__DEV__ = false;
|
|
43
|
+
globalState.__geolocationDevToolsEnabled = true;
|
|
44
|
+
|
|
45
|
+
expect(isDevtoolsEnabled()).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("stays disabled when __DEV__ is unavailable", () => {
|
|
49
|
+
globalState.__DEV__ = undefined;
|
|
50
|
+
globalState.__geolocationDevToolsEnabled = true;
|
|
51
|
+
|
|
52
|
+
expect(isDevtoolsEnabled()).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
});
|
package/src/devtools/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { GeolocationResponse } from "../types";
|
|
2
2
|
|
|
3
|
+
declare const __DEV__: boolean;
|
|
4
|
+
|
|
3
5
|
declare global {
|
|
4
6
|
var __geolocationDevToolsEnabled: boolean | undefined;
|
|
5
7
|
var __geolocationDevtools: DevtoolsState | undefined;
|
|
@@ -18,6 +20,10 @@ export function getDevtoolsState(): DevtoolsState {
|
|
|
18
20
|
return globalThis.__geolocationDevtools;
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
function isReactNativeDev(): boolean {
|
|
24
|
+
return typeof __DEV__ !== "undefined" && __DEV__ === true;
|
|
25
|
+
}
|
|
26
|
+
|
|
21
27
|
export function isDevtoolsEnabled(): boolean {
|
|
22
|
-
return globalThis.__geolocationDevToolsEnabled === true;
|
|
28
|
+
return isReactNativeDev() && globalThis.__geolocationDevToolsEnabled === true;
|
|
23
29
|
}
|