-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate android & ios/web implementation for better readability
- Loading branch information
Showing
3 changed files
with
107 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { NativeEventEmitter, NativeModules } from 'react-native'; | ||
|
||
const { RNFusedLocation } = NativeModules; | ||
const LocationEventEmitter = new NativeEventEmitter(RNFusedLocation); | ||
|
||
const noop = () => {}; | ||
let subscriptions = []; | ||
let updatesEnabled = false; | ||
|
||
const Geolocation = { | ||
setRNConfiguration: (config) => {}, // eslint-disable-line no-unused-vars | ||
|
||
requestAuthorization: () => {}, | ||
|
||
getCurrentPosition: async (success, error = noop, options = {}) => { | ||
if (!success) { | ||
// eslint-disable-next-line no-console | ||
console.error('Must provide a success callback'); | ||
} | ||
|
||
// Right now, we're assuming user already granted location permission. | ||
RNFusedLocation.getCurrentPosition(options, success, error); | ||
}, | ||
|
||
watchPosition: (success, error = null, options = {}) => { | ||
if (!success) { | ||
// eslint-disable-next-line no-console | ||
console.error('Must provide a success callback'); | ||
} | ||
|
||
if (!updatesEnabled) { | ||
RNFusedLocation.startObserving(options); | ||
updatesEnabled = true; | ||
} | ||
|
||
const watchID = subscriptions.length; | ||
|
||
subscriptions.push([ | ||
LocationEventEmitter.addListener('geolocationDidChange', success), | ||
error ? LocationEventEmitter.addListener('geolocationError', error) : null | ||
]); | ||
|
||
return watchID; | ||
}, | ||
|
||
clearWatch: (watchID) => { | ||
const sub = subscriptions[watchID]; | ||
|
||
if (!sub) { | ||
// Silently exit when the watchID is invalid or already cleared | ||
// This is consistent with timers | ||
return; | ||
} | ||
|
||
sub[0].remove(); | ||
|
||
const sub1 = sub[1]; | ||
|
||
if (sub1) { | ||
sub1.remove(); | ||
} | ||
|
||
subscriptions[watchID] = undefined; | ||
|
||
let noWatchers = true; | ||
|
||
for (let ii = 0; ii < subscriptions.length; ii += 1) { | ||
if (subscriptions[ii]) { | ||
noWatchers = false; // still valid subscriptions | ||
} | ||
} | ||
|
||
if (noWatchers) { | ||
Geolocation.stopObserving(); | ||
} | ||
}, | ||
|
||
stopObserving: () => { | ||
if (updatesEnabled) { | ||
RNFusedLocation.stopObserving(); | ||
updatesEnabled = false; | ||
|
||
for (let ii = 0; ii < subscriptions.length; ii += 1) { | ||
const sub = subscriptions[ii]; | ||
if (sub) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Called stopObserving with existing subscriptions.'); | ||
sub[0].remove(); | ||
|
||
const sub1 = sub[1]; | ||
|
||
if (sub1) { | ||
sub1.remove(); | ||
} | ||
} | ||
} | ||
|
||
subscriptions = []; | ||
} | ||
} | ||
}; | ||
|
||
export default Geolocation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Geolocation from '@react-native-community/geolocation'; | ||
|
||
export default Geolocation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters