|
1 | | -/** |
2 | | - * Copyright (c) Facebook, Inc. and its affiliates. |
3 | | - * |
4 | | - * This source code is licensed under the MIT license found in the |
5 | | - * LICENSE file in the root directory of this source tree. |
6 | | - * |
| 1 | +/* |
7 | 2 | * @format |
8 | 3 | * @flow |
9 | 4 | */ |
10 | 5 |
|
11 | | -import {RNCGeolocation, GeolocationEventEmitter} from './nativeInterface'; |
12 | | - |
13 | | -import invariant from 'invariant'; |
14 | | -import {logError, warning} from './utils'; |
15 | | - |
16 | | -let subscriptions = []; |
17 | | -let updatesEnabled = false; |
18 | | - |
19 | | -type GeoConfiguration = { |
20 | | - skipPermissionRequests: boolean, |
21 | | - authorizationLevel: 'always' | 'whenInUse' | 'auto', |
22 | | -}; |
23 | | - |
24 | 6 | type GeoOptions = { |
25 | | - timeout?: number, |
26 | | - maximumAge?: number, |
27 | | - enableHighAccuracy?: boolean, |
28 | | - distanceFilter?: number, |
29 | | - useSignificantChanges?: boolean, |
| 7 | + timeout: number, |
| 8 | + maximumAge: number, |
| 9 | + enableHighAccuracy: boolean, |
30 | 10 | }; |
31 | 11 |
|
32 | | -/** |
33 | | - * The Geolocation API extends the web spec: |
34 | | - * https://developer.mozilla.org/en-US/docs/Web/API/Geolocation |
35 | | - * |
36 | | - * See https://facebook.github.io/react-native/docs/geolocation.html |
37 | | - */ |
38 | 12 | const Geolocation = { |
39 | | - /* |
40 | | - * Sets configuration options that will be used in all location requests. |
41 | | - * |
42 | | - * See https://facebook.github.io/react-native/docs/geolocation.html#setrnconfiguration |
43 | | - * |
44 | | - */ |
45 | | - setRNConfiguration: function(config: GeoConfiguration) { |
46 | | - if (RNCGeolocation.setConfiguration) { |
47 | | - RNCGeolocation.setConfiguration(config); |
48 | | - } |
| 13 | + setRNConfiguration: function() { |
| 14 | + throw new Error('method not supported by the browser'); |
49 | 15 | }, |
50 | | - |
51 | | - /* |
52 | | - * Requests Location permissions based on the key configured on pList. |
53 | | - * |
54 | | - * See https://facebook.github.io/react-native/docs/geolocation.html#requestauthorization |
55 | | - */ |
56 | 16 | requestAuthorization: function() { |
57 | | - RNCGeolocation.requestAuthorization(); |
| 17 | + throw new Error('method not supported by the browser'); |
58 | 18 | }, |
59 | | - |
60 | | - /* |
61 | | - * Invokes the success callback once with the latest location info. |
62 | | - * |
63 | | - * See https://facebook.github.io/react-native/docs/geolocation.html#getcurrentposition |
64 | | - */ |
65 | 19 | getCurrentPosition: async function( |
66 | | - geo_success: Function, |
67 | | - geo_error?: Function, |
68 | | - geo_options?: GeoOptions, |
| 20 | + success: Function, |
| 21 | + error?: Function, |
| 22 | + options?: GeoOptions, |
69 | 23 | ) { |
70 | | - invariant( |
71 | | - typeof geo_success === 'function', |
72 | | - 'Must provide a valid geo_success callback.', |
73 | | - ); |
74 | | - |
75 | | - // Permission checks/requests are done on the native side |
76 | | - RNCGeolocation.getCurrentPosition( |
77 | | - geo_options || {}, |
78 | | - geo_success, |
79 | | - geo_error || logError, |
80 | | - ); |
| 24 | + if (typeof success !== 'function') { |
| 25 | + throw new Error('success callback must be a function'); |
| 26 | + } else if (!navigator || !navigator.geolocation) { |
| 27 | + console.error('Navigator is undefined'); |
| 28 | + return; |
| 29 | + } |
| 30 | + navigator.geolocation.getCurrentPosition(success, error, options); |
81 | 31 | }, |
82 | | - |
83 | | - /* |
84 | | - * Invokes the success callback whenever the location changes. |
85 | | - * |
86 | | - * See https://facebook.github.io/react-native/docs/geolocation.html#watchposition |
87 | | - */ |
88 | 32 | watchPosition: function( |
89 | 33 | success: Function, |
90 | 34 | error?: Function, |
91 | 35 | options?: GeoOptions, |
92 | | - ): number { |
93 | | - if (!updatesEnabled) { |
94 | | - RNCGeolocation.startObserving(options || {}); |
95 | | - updatesEnabled = true; |
| 36 | + ) { |
| 37 | + if (typeof success !== 'function') { |
| 38 | + throw new Error('success callback must be a function'); |
| 39 | + } else if (!navigator || !navigator.geolocation) { |
| 40 | + console.error('Navigator is undefined'); |
| 41 | + return; |
96 | 42 | } |
97 | | - const watchID = subscriptions.length; |
98 | | - subscriptions.push([ |
99 | | - GeolocationEventEmitter.addListener('geolocationDidChange', success), |
100 | | - error |
101 | | - ? GeolocationEventEmitter.addListener('geolocationError', error) |
102 | | - : null, |
103 | | - ]); |
104 | | - return watchID; |
| 43 | + navigator.geolocation.watchPosition(success, error, options); |
105 | 44 | }, |
106 | | - |
107 | | - /* |
108 | | - * Unsubscribes the watcher with the given watchID. |
109 | | - * |
110 | | - * See https://facebook.github.io/react-native/docs/geolocation.html#clearwatch |
111 | | - */ |
112 | 45 | clearWatch: function(watchID: number) { |
113 | | - const sub = subscriptions[watchID]; |
114 | | - if (!sub) { |
115 | | - // Silently exit when the watchID is invalid or already cleared |
116 | | - // This is consistent with timers |
| 46 | + if (!navigator || !navigator.geolocation) { |
| 47 | + console.error('Navigator is undefined'); |
117 | 48 | return; |
118 | 49 | } |
119 | | - |
120 | | - sub[0].remove(); |
121 | | - // array element refinements not yet enabled in Flow |
122 | | - const sub1 = sub[1]; |
123 | | - sub1 && sub1.remove(); |
124 | | - subscriptions[watchID] = undefined; |
125 | | - let noWatchers = true; |
126 | | - for (let ii = 0; ii < subscriptions.length; ii++) { |
127 | | - if (subscriptions[ii]) { |
128 | | - noWatchers = false; // still valid subscriptions |
129 | | - } |
130 | | - } |
131 | | - if (noWatchers) { |
132 | | - Geolocation.stopObserving(); |
133 | | - } |
| 50 | + navigator.geolocation.clearWatch(watchID); |
134 | 51 | }, |
135 | | - |
136 | | - /* |
137 | | - * Stops observing for device location changes and removes all registered listeners. |
138 | | - * |
139 | | - * See https://facebook.github.io/react-native/docs/geolocation.html#stopobserving |
140 | | - */ |
141 | 52 | stopObserving: function() { |
142 | | - if (updatesEnabled) { |
143 | | - RNCGeolocation.stopObserving(); |
144 | | - updatesEnabled = false; |
145 | | - for (let ii = 0; ii < subscriptions.length; ii++) { |
146 | | - const sub = subscriptions[ii]; |
147 | | - if (sub) { |
148 | | - warning(false, 'Called stopObserving with existing subscriptions.'); |
149 | | - sub[0].remove(); |
150 | | - // array element refinements not yet enabled in Flow |
151 | | - const sub1 = sub[1]; |
152 | | - sub1 && sub1.remove(); |
153 | | - } |
154 | | - } |
155 | | - subscriptions = []; |
156 | | - } |
| 53 | + throw new Error('method not supported by the browser'); |
157 | 54 | }, |
158 | 55 | }; |
159 | | - |
160 | 56 | module.exports = Geolocation; |
0 commit comments