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
var exec = require('cordova/exec');
var baidumap_location = {
getCurrentPosition: function (successCallback, errorCallback, options) {
// Timer var that will fire an error callback if no position is retrieved from native
// before the "timeout" param provided expires
var timeoutTimer = {timer: null};
var win = function (p) {
clearTimeout(timeoutTimer.timer);
if (!(timeoutTimer.timer)) {
// Timeout already happened, or native fired error callback for
// this geo request.
// Don't continue with success callback.
return;
}
successCallback(p);
};
var fail = function (e) {
clearTimeout(timeoutTimer.timer);
timeoutTimer.timer = null;
if (errorCallback) {
errorCallback(e);
}
};
if (options && options.timeout !== Infinity) {
// If the timeout value was not set to Infinity (default), then
// set up a timeout function that will fire the error callback
// if no successful position was retrieved before timeout expired.
timeoutTimer.timer = createTimeout(fail, options.timeout);
} else {
// This is here so the check in the win function doesn't mess stuff up
// may seem weird but this guarantees timeoutTimer is
// always truthy before we call into native
timeoutTimer.timer = true;
}
exec(win, fail, 'BaiduMapLocation', 'getCurrentPosition', [options]);
return timeoutTimer
}
};
// Returns a timeout failure, closed over a specified timeout value and error callback.
function createTimeout(errorCallback, timeout) {
var t = setTimeout(function () {
clearTimeout(t);
t = null;
errorCallback({
code: -1,
message: "Position retrieval timed out."
});
}, timeout);
return t;
}
module.exports = baidumap_location