Guest User

Untitled

a guest
Oct 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. // todo:@flow
  2.  
  3. import { AsyncStorage, Platform } from 'react-native';
  4. import * as Debug from 'App/src/Debug';
  5. import { toStringify } from 'App/src/utils/Utils';
  6. import { addPrefix } from 'App/src/utils/AsyncStorage';
  7.  
  8. const location_key = addPrefix('location_enabled')
  9.  
  10. export const ENABLE_LOCATION = true;
  11. export const DISABLE_LOCATION = false;
  12. let enableLocation:boolean = DISABLE_LOCATION;
  13. let position:any = {};
  14. let watchInterval: any;
  15.  
  16. export const getEnableLocation = () => {
  17. return enableLocation;
  18. };
  19.  
  20. const locationOptions = {
  21. enableHighAccuracy: true,
  22. timeout: 5000, // 5 sec
  23. maximumAge: 30*60*1000, // Upto 30 minutes
  24. distanceFilter: 100, // meters
  25. };
  26.  
  27. export async function init() {
  28. Debug.logFn(() => `Location.init()...`);
  29. try {
  30. const enabled = await AsyncStorage.getItem(location_key);
  31. if (enabled === 'true') {
  32. enableLocation = ENABLE_LOCATION
  33. startLocationUpdates()
  34. }
  35. }
  36. catch (err) {
  37. Debug.logError(`Location.init(): error ${toStringify(err)}`);
  38. }
  39. Debug.logFn(() => `Location.init(): ...enableLocation ${enableLocation.toString()}`);
  40. }
  41.  
  42. const startLocationUpdates = () => {
  43. if (Platform.OS === 'ios') {
  44. navigator.geolocation.requestAuthorization();
  45. }
  46. watchInterval = () => {
  47. navigator.geolocation.getCurrentPosition(onLocationSuccess, onLocationError, locationOptions);
  48. setInterval(watchInterval, 1800 * 1000)
  49. }
  50. watchInterval()
  51. Debug.logFn(() => `Watching position`);
  52. }
  53.  
  54. export const setEnableLocation = (locOnOff:boolean) => {
  55. Debug.logFn(() => `setEnableLocation(): ${locOnOff.toString()}`);
  56. if (locOnOff && !enableLocation) {
  57. startLocationUpdates()
  58. }
  59. if (!locOnOff && enableLocation) {
  60. Debug.logFn(() => `Clearing location watch`);
  61. clearInterval(watchInterval);
  62. }
  63. enableLocation = locOnOff;
  64. AsyncStorage.setItem(location_key, locOnOff.toString())
  65. };
  66.  
  67. export const getLastLocation = () => {
  68. return position;
  69. };
  70.  
  71. export const getLocationData = () => {
  72. let data;
  73. if (getEnableLocation()) {
  74. const pos:any = getLastLocation();
  75. if (pos && pos.coords) {
  76. data = {
  77. last_known_location: {
  78. lat: pos.coords.latitude,
  79. lon: pos.coords.longitude,
  80. accuracy_meters: pos.coords.accuracy,
  81. timestamp_seconds: pos.timestamp/1000.0,
  82. }
  83. };
  84. }
  85. }
  86. return data;
  87. };
  88.  
  89. const onLocationSuccess = (pos:any) => {
  90. Debug.logFn(() => `Got location update: ${JSON.stringify(pos)}`);
  91. position = pos;
  92. };
  93.  
  94. const onLocationError = (err:any) => {
  95. Debug.logFn(() => `onLocationError: ${JSON.stringify(err)}`);
  96. if (enableLocation && err.PERMISSION_DENIED === 1) {
  97. startLocationUpdates()
  98. }
  99. Debug.logError(`Location request failed`, err);
  100. };
Add Comment
Please, Sign In to add comment