UNPKG

3.77 kBJavaScriptView Raw
1import * as UIA from './actions/uiActions';
2import GEO from './geocodeservice';
3import * as Globals from './globals';
4import * as GLOBALS from './global';
5import {
6 Platform,
7 PermissionsAndroid
8} from 'react-native';
9import * as Titles from './titles';
10import ApiService from './apiservice';
11import GeoCodeService from './geocodeservice';
12import * as Util from './redutil';
13import * as Common from './common';
14export const CURRENT_ADDRESS = 'CURRENT_ADDRESS';
15export const CURRENT_ADDRESS_NAME = 'CURRENT_ADDRESS_NAME';
16export const CURRENT_LOCATION = 'CURRENT_LOCATION';
17var lastlocation = null;
18var promise = Promise.resolve();
19var catcher = e => {
20 if (e && e.message && e.message.json) {
21 return e.message.json().then(c => UIA.log(c));
22 }
23 UIA.log(e);
24};
25var setkey = false;
26var stopped = true;
27var $geolocationid = null;
28var geolocationid = null;
29export default {
30 stop: () => {
31 if (geolocationid) {
32 navigator.geolocation.clearWatch(geolocationid);
33 }
34 if ($geolocationid) {
35 navigator.geolocation.clearWatch($geolocationid);
36 }
37 stopped = true;
38 },
39 start: (dispatch, getState, islaborer) => {
40 var crd;
41 if (!stopped) {
42 return true;
43 }
44 stopped = false;
45 var nextLocationUpdateTime = Date.now() - GLOBALS.LOCATION_UPDATE_THROTTLE;
46 function success(pos) {
47 crd = pos.coords ? pos.coords : pos;
48 var distance = Util.getDistance(crd, lastlocation);
49 if (lastlocation && distance < 40) {
50 return;
51 }
52 lastlocation = crd;
53 dispatch(UIA.UI(CURRENT_LOCATION, Object.assign({
54 latitudeDelta: .013,
55 longitudeDelta: 0.013
56 }, crd)));
57 if (Date.now() > nextLocationUpdateTime) {
58 nextLocationUpdateTime = Date.now() + GLOBALS.LOCATION_UPDATE_THROTTLE;
59 }
60 else {
61 return nextLocationUpdateTime;
62 }
63
64 promise = promise.then(() => {
65 if (crd)
66 return Globals.setLocation(crd);
67 });
68 promise = promise.then(() => {
69 if (!setkey) {
70 setkey = true;
71 return ApiService.getApiKey().then((apiKey) => {
72 GeoCodeService.setKey(apiKey);
73 });
74 }
75 }).then(() => {
76 // return GEO.getGeoCodes(crd.latitude, crd.longitude).then(location => {
77 // var state = getState();
78 // var startaddress = Common.getAddress({
79 // props: {
80 // state
81 // }
82 // }, location, null, null, null);
83
84 // var address_name = Common.getAddressName(location);
85 // if (startaddress) {
86 // dispatch(UIA.UI(CURRENT_ADDRESS, startaddress));
87 // dispatch(UIA.UI(CURRENT_ADDRESS_NAME, address_name))
88 // }
89 // });
90 });
91 }
92
93 function error(err) {
94 console.log('ERROR(' + err.code + '): ' + err.message);
95 }
96
97 options = {
98 enableHighAccuracy: true,
99 timeout: GLOBALS.LOCATION_UPDATE_THROTTLE,
100 distanceFilter: 1,
101 maximumAge: GLOBALS.LOCATION_UPDATE_THROTTLE
102 };
103 if (Platform.OS === 'android') {
104 geolocationid = navigator.geolocation.watchPosition(success, error, options);
105 }
106 else {
107 geolocationid = navigator.geolocation.watchPosition(success, error, options);
108 }
109 }
110}
\No newline at end of file