]>
git.proxmox.com Git - extjs.git/blob - extjs/modern/modern/src/util/Geolocation.js
2 * Provides a cross browser class for retrieving location information.
4 * Based on the [Geolocation API Specification](http://dev.w3.org/geo/api/spec-source.html)
6 * When instantiated, by default this class immediately begins tracking location information,
7 * firing a {@link #locationupdate} event when new location information is available. To disable this
8 * location tracking (which may be battery intensive on mobile devices), set {@link #autoUpdate} to `false`.
10 * When this is done, only calls to {@link #updateLocation} will trigger a location retrieval.
12 * A {@link #locationerror} event is raised when an error occurs retrieving the location, either due to a user
13 * denying the application access to it, or the browser not supporting it.
15 * The below code shows a GeoLocation making a single retrieval of location information.
17 * var geo = Ext.create('Ext.util.Geolocation', {
20 * locationupdate: function(geo) {
21 * alert('New latitude: ' + geo.getLatitude());
23 * locationerror: function(geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) {
25 * alert('Timeout occurred.');
27 * alert('Error occurred.');
32 * geo.updateLocation();
34 Ext
.define('Ext.util.Geolocation', {
35 extend
: 'Ext.Evented',
36 alternateClassName
: ['Ext.util.GeoLocation'],
40 * @event locationerror
41 * Raised when a location retrieval operation failed.
43 * In the case of calling updateLocation, this event will be raised only once.
45 * If {@link #autoUpdate} is set to `true`, this event could be raised repeatedly.
46 * The first error is relative to the moment {@link #autoUpdate} was set to `true`
47 * (or this {@link Ext.util.Geolocation} was initialized with the {@link #autoUpdate} config option set to `true`).
48 * Subsequent errors are relative to the moment when the device determines that it's position has changed.
49 * @param {Ext.util.Geolocation} this
50 * @param {Boolean} timeout
51 * Boolean indicating a timeout occurred
52 * @param {Boolean} permissionDenied
53 * Boolean indicating the user denied the location request
54 * @param {Boolean} locationUnavailable
55 * Boolean indicating that the location of the device could not be determined.
56 * For instance, one or more of the location providers used in the location acquisition
57 * process reported an internal error that caused the process to fail entirely.
58 * @param {String} message An error message describing the details of the error encountered.
60 * This attribute is primarily intended for debugging and should not be used
61 * directly in an application user interface.
65 * @event locationupdate
66 * Raised when a location retrieval operation has been completed successfully.
67 * @param {Ext.util.Geolocation} this
68 * Retrieve the current location information from the GeoLocation object by using the read-only
69 * properties: {@link #latitude}, {@link #longitude}, {@link #accuracy}, {@link #altitude}, {@link #altitudeAccuracy}, {@link #heading}, and {@link #speed}.
73 * @cfg {Boolean} autoUpdate
74 * When set to `true`, continually monitor the location of the device (beginning immediately)
75 * and fire {@link #locationupdate} and {@link #locationerror} events.
80 * @cfg {Number} frequency
81 * The frequency of each update if {@link #autoUpdate} is set to `true`.
86 * @cfg {Number} latitude
87 * Read-only property representing the last retrieved
88 * geographical coordinate specified in degrees.
94 * @cfg {Number} longitude
95 * Read-only property representing the last retrieved
96 * geographical coordinate specified in degrees.
102 * @cfg {Number} accuracy
103 * Read-only property representing the last retrieved
104 * accuracy level of the latitude and longitude coordinates,
105 * specified in meters.
107 * This will always be a non-negative number.
109 * This corresponds to a 95% confidence level.
115 * @cfg {Number} altitude
116 * Read-only property representing the last retrieved
117 * height of the position, specified in meters above the ellipsoid
118 * [WGS84](http://dev.w3.org/geo/api/spec-source.html#ref-wgs).
124 * @cfg {Number} altitudeAccuracy
125 * Read-only property representing the last retrieved
126 * accuracy level of the altitude coordinate, specified in meters.
128 * If altitude is not null then this will be a non-negative number.
129 * Otherwise this returns `null`.
131 * This corresponds to a 95% confidence level.
134 altitudeAccuracy
: null,
137 * @cfg {Number} heading
138 * Read-only property representing the last retrieved
139 * direction of travel of the hosting device,
140 * specified in non-negative degrees between 0 and 359,
141 * counting clockwise relative to the true north.
143 * If speed is 0 (device is stationary), then this returns `NaN`.
149 * @cfg {Number} speed
150 * Read-only property representing the last retrieved
151 * current ground speed of the device, specified in meters per second.
153 * If this feature is unsupported by the device, this returns `null`.
155 * If the device is stationary, this returns 0,
156 * otherwise it returns a non-negative number.
162 * @cfg {Date} timestamp
163 * Read-only property representing when the last retrieved
164 * positioning information was acquired by the device.
169 //PositionOptions interface
171 * @cfg {Boolean} allowHighAccuracy
172 * When set to `true`, provide a hint that the application would like to receive
173 * the best possible results. This may result in slower response times or increased power consumption.
174 * The user might also deny this capability, or the device might not be able to provide more accurate
175 * results than if this option was set to `false`.
177 allowHighAccuracy
: false,
180 * @cfg {Number} timeout
181 * The maximum number of milliseconds allowed to elapse between a location update operation
182 * and the corresponding {@link #locationupdate} event being raised. If a location was not successfully
183 * acquired before the given timeout elapses (and no other internal errors have occurred in this interval),
184 * then a {@link #locationerror} event will be raised indicating a timeout as the cause.
186 * Note that the time that is spent obtaining the user permission is **not** included in the period
187 * covered by the timeout. The `timeout` attribute only applies to the location acquisition operation.
189 * In the case of calling `updateLocation`, the {@link #locationerror} event will be raised only once.
191 * If {@link #autoUpdate} is set to `true`, the {@link #locationerror} event could be raised repeatedly.
192 * The first timeout is relative to the moment {@link #autoUpdate} was set to `true`
193 * (or this {@link Ext.util.Geolocation} was initialized with the {@link #autoUpdate} config option set to `true`).
194 * Subsequent timeouts are relative to the moment when the device determines that it's position has changed.
199 * @cfg {Number} maximumAge
200 * This option indicates that the application is willing to accept cached location information whose age
201 * is no greater than the specified time in milliseconds. If `maximumAge` is set to 0, an attempt to retrieve
202 * new location information is made immediately.
204 * Setting the `maximumAge` to Infinity returns a cached position regardless of its age.
206 * If the device does not have cached location information available whose age is no
207 * greater than the specified `maximumAge`, then it must acquire new location information.
209 * For example, if location information no older than 10 minutes is required, set this property to 600000.
219 updateMaximumAge: function() {
220 if (this.watchOperation
) {
221 this.updateWatchOperation();
225 updateTimeout: function() {
226 if (this.watchOperation
) {
227 this.updateWatchOperation();
231 updateAllowHighAccuracy: function() {
232 if (this.watchOperation
) {
233 this.updateWatchOperation();
237 applyProvider: function(config
) {
238 if (Ext
.feature
.has
.Geolocation
) {
240 if (navigator
&& navigator
.geolocation
) {
241 config
= navigator
.geolocation
;
243 else if (window
.google
) {
244 config
= google
.gears
.factory
.create('beta.geolocation');
249 this.fireEvent('locationerror', this, false, false, true, 'This device does not support Geolocation.');
254 updateAutoUpdate: function(newAutoUpdate
, oldAutoUpdate
) {
256 provider
= me
.getProvider();
258 if (oldAutoUpdate
&& provider
) {
259 clearInterval(me
.watchOperationId
);
260 me
.watchOperationId
= null;
265 me
.fireEvent('locationerror', me
, false, false, true, null);
270 me
.updateWatchOperation();
273 me
.fireEvent('locationerror', me
, false, false, true, e
.message
);
281 updateWatchOperation: function() {
283 provider
= me
.getProvider();
285 // The native watchPosition method is currently broken in iOS5...
287 if (me
.watchOperationId
) {
288 clearInterval(me
.watchOperationId
);
291 function pollPosition() {
292 provider
.getCurrentPosition(
293 Ext
.bind(me
.fireUpdate
, me
),
294 Ext
.bind(me
.fireError
, me
),
300 me
.watchOperationId
= Ext
.interval(pollPosition
, this.getFrequency());
304 * Executes a onetime location update operation,
305 * raising either a {@link #locationupdate} or {@link #locationerror} event.
307 * Does not interfere with or restart ongoing location monitoring.
308 * @param {Function} callback
309 * A callback method to be called when the location retrieval has been completed.
311 * Will be called on both success and failure.
313 * The method will be passed one parameter, {@link Ext.util.Geolocation}
314 * (**this** reference), set to `null` on failure.
316 * geo.updateLocation(function (geo) {
317 * alert('Latitude: ' + (geo !== null ? geo.latitude : 'failed'));
320 * @param {Object} [scope]
321 * The scope (**this** reference) in which the handler function is executed.
323 * **If omitted, defaults to the object which fired the event.**
325 * <!--positonOptions undocumented param, see W3C spec-->
327 updateLocation: function(callback
, scope
, positionOptions
) {
329 provider
= me
.getProvider();
331 var failFunction = function(message
, error
) {
336 me
.fireEvent('locationerror', me
, false, false, true, message
);
339 callback
.call(scope
|| me
, null, me
); //last parameter for legacy purposes
349 provider
.getCurrentPosition(
352 me
.fireUpdate(position
);
354 callback
.call(scope
|| me
, me
, me
); //last parameter for legacy purposes
359 failFunction(null, error
);
361 positionOptions
|| me
.parseOptions()
365 failFunction(e
.message
);
372 fireUpdate: function(position
) {
374 coords
= position
.coords
;
376 this.position
= position
;
379 timestamp
: position
.timestamp
,
380 latitude
: coords
.latitude
,
381 longitude
: coords
.longitude
,
382 accuracy
: coords
.accuracy
,
383 altitude
: coords
.altitude
,
384 altitudeAccuracy
: coords
.altitudeAccuracy
,
385 heading
: coords
.heading
,
389 me
.fireEvent('locationupdate', me
);
395 fireError: function(error
) {
396 var errorCode
= error
.code
;
397 this.fireEvent('locationerror', this,
398 errorCode
== error
.TIMEOUT
,
399 errorCode
== error
.PERMISSION_DENIED
,
400 errorCode
== error
.POSITION_UNAVAILABLE
,
401 error
.message
== undefined ? null : error
.message
408 parseOptions: function() {
409 var timeout
= this.getTimeout(),
411 maximumAge
: this.getMaximumAge(),
412 enableHighAccuracy
: this.getAllowHighAccuracy()
415 //Google doesn't like Infinity
416 if (timeout
!== Infinity
) {
417 ret
.timeout
= timeout
;
422 destroy: function() {
423 this.setAutoUpdate(false);