-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcordova-geolocation.js
88 lines (82 loc) · 2.13 KB
/
cordova-geolocation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Geolocation API for foreground actions
*
* basically a wrapper for
* http://plugins.cordova.io/#/package/org.apache.cordova.geolocation
*
* - get()
* - watch()
* - clearWatch()
*/
GeolocationFG = GeolocationFG || {};
/**
* Default options
*/
GeolocationFG.options = GeolocationFG.options || {
maximumAge: 3000,
timeout: 5000,
enableHighAccuracy: true
};
/**
* Global and generic onError handler for foreground actions
*
* @param object error
* @return void
*/
GeolocationFG.onError = function(error) {
if (!_.isObject(error)) {
throw new Meteor.Error(500, 'GeolocationFG error' . error);
return;
}
throw new Meteor.Error(500, 'GeolocationFG error [' + error.code + '] ' + error.message);
};
/**
* Get the current Geolocation now, in the foreground
*
* @param function onSuccess
* @param object options
* @return boolean
*/
GeolocationFG.get = function(onSuccess, options) {
if (!_.isFunction(onSuccess)) {
throw new Meteor.Error(500, 'GeolocationFG.get onSuccess is not a function');
}
if (!_.isObject(options)) {
options = GeolocationFG.options;
}
return navigator.geolocation.getCurrentPosition(onSuccess, GeolocationFG.onError, options);
};
/**
* Start a foreground "watch" for Geolocation
*
* @param function onSuccess
* @param int timeout
* @param object options
* @return string watchId
*/
GeolocationFG.watch = function(onSuccess, timeout, options) {
if (!_.isFunction(onSuccess)) {
throw new Meteor.Error(500, 'GeolocationFG.watch onSuccess is not a function');
}
//check if timeout is an integer
if ( !(parseInt(timeout, 10) == timeout) ) {
timeout = 30000;
}
if (!_.isObject(options)) {
options = GeolocationFG.options;
}
options.timeout = timeout;
return navigator.geolocation.watchPosition(onSuccess, GeolocationFG.onError, options);
};
/**
* Clear a foreground "watch" for Geolocation
*
* @param string watchId
* @return void
*/
GeolocationFG.clearWatch = function(watchId) {
if (!_.isString(watchId)) {
throw new Meteor.Error(500, 'GeolocationFG.clearWatch watchId is not a string');
}
return navigator.geolocation.clearWatch(watchId);
};