geolocator.js
5.56 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
arcs_module(function(ARCS) {
/**
* @class GeoLocator
* @classdesc Uses the location provider of the browser to
* determine the geolocation of the device.
* This component relies on the
* {@link http://www.w3.org/TR/geolocation-API/|Geolocation API}.
*/
var GeoLocator = ARCS.Component.create(function() {
var self = this;
var geolocator = navigator.geolocation ;
var watcher;
var timeout = 1000;
var enableHighAccuracy = false;
var handlePosition = function(position) {
self.emit("sendPosition",position.coords.longitude,position.coords.latitude);
self.emit("sendCoordinates", position);
};
var handleError = function(positionError) {
switch(positionError.code) {
case positionError.TIMEOUT:
console.error("Timeout while geolocalizing", positionError.message);
break;
case positionError.PERMISSION_DENIED:
console.error("You must authorize geolocation", positionError.message);
break;
case positionError.POSITION_UNAVAILABLE:
console.error("Position is unavailable", positionError.message);
break;
}
self.emit("geolocationDisabled");
};
/**
* Set the timeout (in milliseconds) between the call of
* {@link GeoLocator#getPosition|getPosition} or
* {@link GeoLocator#watchPosition|watchPosition} and
* the sending of signal {@link GeoLocator#sendPosition|sendPosition}.
* @param t {number} timeout duration in milliseconds
* @function GeoLocator#setTimeout
* @slot
*/
this.setTimeout = function(t) {
timeout = t;
};
this.enableHighAccuracy = function() {
enableHighAccuracy = true;
};
this.disableHighAccuracy = function() {
enableHighAccuracy = false;
};
/**
* Retrieves the geolocation of the device. Use this function for a
* one shot retrieval. If it succeeds, signals {@link GeoLocator#sendPosition|sendPosition}
* and {@link GeoLocator#sendCoordinates|sendCoordinates} are triggered.
* Otherwise, signal {@link GeoLocator#geolocationDisabled|geolocationDisabled} is triggered.
* @emits sendPosition
* @emits sendCoordinates
* @emits geolocationDisabled
* @function GeoLocator#getPosition
* @slot
*/
this.getPosition = function() {
if (geolocator) {
geolocator.getCurrentPosition(handlePosition,handleError,
{ timeout: timeout, enableHighAccuracy : enableHighAccuracy} );
} else {
this.emit("geolocationDisabled");
}
};
/**
* Retrieves the geolocation of the device. Use this function if you
* want to continuously obtain the geolocation of the device.
* If it succeeds, signals {@link GeoLocator#sendPosition|sendPosition}
* and {@link GeoLocator#sendCoordinates|sendCoordinates} are triggered.
* Otherwise, signal {@link GeoLocator#geolocationDisabled|geoLocationDisabled} is triggered.
* You can use {@link clearWatch} to stop watching the position.
* @emits sendPosition
* @emits sendCoordinates
* @emits geolocationDisabled
* @function GeoLocator#watchPosition
* @slot
*/
this.watchPosition= function() {
if (geolocator) {
if (watcher) {
geolocator.clearWatch(watcher);
}
watcher = geolocator.watchPosition(handlePosition, handleError,
{ timeout: timeout, enableHighAccuracy : enableHighAccuracy} );
} else {
this.emit("geolocationDisabled");
}
};
/**
* Ends geolocation queries.
* @see {@link GeoLocator#watchPosition|watchPosition}
* @function GeoLocator#clearWatch
* @slot
*/
this.clearWatch = function() {
if (geolocator) {
if (watcher) {
geolocator.clearWatch(watcher);
}
}
};
/**
* Sends the position of the device (longitude and latitude)
* @param lon {number} longitude of the geolocation
* @param lat {number} latitude of the geolocation
* @function GeoLocator#sendPosition
* @signal
*/
/**
* Sends the coordinates of the device using the coordinates object
* proposed in the specification of the geolocation API.
* @param coords {object} coordinates of the device.
* @function GeoLocator#sendCoordinates
* @signal
*/
/**
* Notifies that the geolocation service is disabled
* @function GeoLocator#geolocationDisabled
* @signal
*/
},
[
"getPosition","watchPosition","clearWatch","setTimeout",
"enableHighAccuracy", "disableHighAccuracy"
], // liste slots
["sendPosition","sendCoordinates","geolocationDisabled"] // liste de signaux
);
return { GeoLocator : GeoLocator } ;
});