-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
geolocation.js
125 lines (110 loc) · 3.88 KB
/
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* This class provides access to device GPS data.
* @constructor
*/
function Geolocation() {
/**
* The last known GPS position.
*/
this.lastPosition = null;
this.lastError = null;
this.callbacks = {
onLocationChanged: [],
onError: []
};
};
/**
* Asynchronously aquires the current position.
* @param {Function} successCallback The function to call when the position
* data is available
* @param {Function} errorCallback The function to call when there is an error
* getting the position data.
* @param {PositionOptions} options The options for getting the position data
* such as timeout.
*/
Geolocation.prototype.getCurrentPosition = function(successCallback, errorCallback, options) {
var referenceTime = 0;
if (this.lastPosition)
referenceTime = this.lastPosition.timeout;
else
this.start(options);
var timeout = 20000;
var interval = 500;
if (typeof(options) == 'object' && options.interval)
interval = options.interval;
if (typeof(successCallback) != 'function')
successCallback = function() {};
if (typeof(errorCallback) != 'function')
errorCallback = function() {};
var dis = this;
var delay = 0;
var timer = setInterval(function() {
delay += interval;
if (typeof(dis.lastPosition) == 'object' && dis.lastPosition.timestamp > referenceTime) {
successCallback(dis.lastPosition);
clearInterval(timer);
} else if (delay >= timeout) {
errorCallback();
clearInterval(timer);
}
}, interval);
};
/**
* Asynchronously aquires the position repeatedly at a given interval.
* @param {Function} successCallback The function to call each time the position
* data is available
* @param {Function} errorCallback The function to call when there is an error
* getting the position data.
* @param {PositionOptions} options The options for getting the position data
* such as timeout and the frequency of the watch.
*/
Geolocation.prototype.watchPosition = function(successCallback, errorCallback, options) {
// Invoke the appropriate callback with a new Position object every time the implementation
// determines that the position of the hosting device has changed.
this.getCurrentPosition(successCallback, errorCallback, options);
var frequency = 10000;
if (typeof(options) == 'object' && options.frequency)
frequency = options.frequency;
var that = this;
return setInterval(function() {
that.getCurrentPosition(successCallback, errorCallback, options);
}, frequency);
};
/**
* Clears the specified position watch.
* @param {String} watchId The ID of the watch returned from #watchPosition.
*/
Geolocation.prototype.clearWatch = function(watchId) {
clearInterval(watchId);
};
/**
* Called by the geolocation framework when the current location is found.
* @param {PositionOptions} position The current position.
*/
Geolocation.prototype.setLocation = function(position) {
this.lastPosition = position;
for (var i = 0; i < this.callbacks.onLocationChanged.length; i++) {
var f = this.callbacks.onLocationChanged.shift();
f(position);
}
};
/**
* Called by the geolocation framework when an error occurs while looking up the current position.
* @param {String} message The text of the error message.
*/
Geolocation.prototype.setError = function(message) {
this.lastError = message;
for (var i = 0; i < this.callbacks.onError.length; i++) {
var f = this.callbacks.onError.shift();
f(message);
}
};
Geolocation.prototype.start = function(args) {
PhoneGap.exec("Location.startLocation", args);
};
Geolocation.prototype.stop = function() {
PhoneGap.exec("Location.stopLocation");
};
PhoneGap.addConstructor(function() {
if (typeof navigator.geolocation == "undefined") navigator.geolocation = new Geolocation();
});