1 | import * as UIA from './actions/uiActions';
|
2 | import GEO from './geocodeservice';
|
3 | import * as Globals from './globals';
|
4 | import * as GLOBALS from './global';
|
5 | import {
|
6 | Platform,
|
7 | PermissionsAndroid
|
8 | } from 'react-native';
|
9 | import * as Titles from './titles';
|
10 | import ApiService from './apiservice';
|
11 | import GeoCodeService from './geocodeservice';
|
12 | import * as Util from './redutil';
|
13 | import * as Common from './common';
|
14 | export const CURRENT_ADDRESS = 'CURRENT_ADDRESS';
|
15 | export const CURRENT_ADDRESS_NAME = 'CURRENT_ADDRESS_NAME';
|
16 | export const CURRENT_LOCATION = 'CURRENT_LOCATION';
|
17 | var lastlocation = null;
|
18 | var promise = Promise.resolve();
|
19 | var 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 | };
|
25 | var setkey = false;
|
26 | var stopped = true;
|
27 | var $geolocationid = null;
|
28 | var geolocationid = null;
|
29 | export 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 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
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 |