[Commits] r2555 - in sandbox/cmoullet/ux/GeolocationAPI: examples lib/GeoExt.ux/control

commits at geoext.org commits at geoext.org
Wed Jan 12 06:16:25 CET 2011


Author: cmoullet
Date: 2011-01-12 06:16:24 +0100 (Wed, 12 Jan 2011)
New Revision: 2555

Modified:
   sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.js
   sandbox/cmoullet/ux/GeolocationAPI/lib/GeoExt.ux/control/GeolocationAPI.js
Log:
Add tracking and error management

Modified: sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.js
===================================================================
--- sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.js	2011-01-11 16:31:50 UTC (rev 2554)
+++ sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.js	2011-01-12 05:16:24 UTC (rev 2555)
@@ -4,7 +4,7 @@
     Ext.QuickTips.init();
 
     var map = new OpenLayers.Map();
-    var GeolocationAPIControl = new OpenLayers.Control.GeolocationAPI({mode: 'position', zoom: 11});
+    var GeolocationAPIControl = new OpenLayers.Control.GeolocationAPI({mode: 'tracking', zoom: 11, geolocationOptions: {timeout:3000}});
     map.addControl(GeolocationAPIControl);
 
     var mapPanel = new GeoExt.MapPanel({
@@ -22,6 +22,14 @@
     GeolocationAPIControl.events.register("positioncomputed", GeolocationAPIControl, function(position) {
         document.getElementById("positionInfo").innerHTML = this.getPositionInformation("<BR>");
     });
+    GeolocationAPIControl.events.register("positionerror", GeolocationAPIControl, function(errorEvent) {
+        switch (errorEvent.error.code) {
+                case 0: document.getElementById("positionInfo").innerHTML = "There was an error while retrieving your location."; break;
+                case 1: document.getElementById("positionInfo").innerHTML = "The user didn't accept to provide a location"; break;
+                case 2: document.getElementById("positionInfo").innerHTML = "The browser was unable to determine your location"; break;
+                case 3: document.getElementById("positionInfo").innerHTML = "The browser timed out before retrieving the location."; break;
+            }
+    });
     GeolocationAPIControl.activate();
 
 });
\ No newline at end of file

Modified: sandbox/cmoullet/ux/GeolocationAPI/lib/GeoExt.ux/control/GeolocationAPI.js
===================================================================
--- sandbox/cmoullet/ux/GeolocationAPI/lib/GeoExt.ux/control/GeolocationAPI.js	2011-01-11 16:31:50 UTC (rev 2554)
+++ sandbox/cmoullet/ux/GeolocationAPI/lib/GeoExt.ux/control/GeolocationAPI.js	2011-01-12 05:16:24 UTC (rev 2555)
@@ -21,13 +21,17 @@
 
     geolocationOptions: {},
 
+    trackingId: false,
+
+    errorAlert: false,
+
     /**
      * Constant: EVENT_TYPES
      *
      * Supported event types:
      * positioncomputed - Triggered when position has been computed
      */
-    EVENT_TYPES: ["positioncomputed"],
+    EVENT_TYPES: ["positioncomputed", "positionerror"],
 
     initialize: function(options) {
         options = options || {};
@@ -52,14 +56,24 @@
         if (this.isGeolocationAPISupported && this.mode == "position") {
             this.activatePosition();
         }
+        if (this.isGeolocationAPISupported && this.mode == "tracking") {
+            this.activateTracking();
+        }
         OpenLayers.Control.prototype.activate.apply(this);
     },
     deactivate: function () {
+        if (this.trackingId) {
+            navigator.geolocation.clearWatch(this.trackingId);
+            this.trackingId = false;
+        }
         OpenLayers.Control.prototype.deactivate.apply(this);
     },
     activatePosition: function() {
-        navigator.geolocation.getCurrentPosition(this.centerMap.bind(this), this.locationError, this.geolocationOptions);
+        navigator.geolocation.getCurrentPosition(this.centerMap.bind(this), this.locationError.bind(this), this.geolocationOptions);
     },
+    activateTracking: function() {
+        this.trackingId = navigator.geolocation.watchPosition(this.centerMap.bind(this), this.locationError.bind(this), this.geolocationOptions);
+    },
     centerMap: function(position) {
         this.currentPosition = position;
         var lonLat = new OpenLayers.LonLat(position.coords.longitude, position.coords.latitude);
@@ -70,13 +84,15 @@
     },
 
     locationError: function(error) {
-        switch (error.code) {
-            case 0: alert("There was an error while retrieving your location: " + error.message); break;
-            case 1: /*The user didn't accept to provide the location */ break;
-            case 2: alert("The browser was unable to determine your location: " + error.message); break;
-            case 3: alert("The browser timed out before retrieving the location."); break;
+        this.events.triggerEvent("positionerror", {error : error});
+        if (this.errorAlert) {
+            switch (error.code) {
+                case 0: alert("There was an error while retrieving your location: " + error.message); break;
+                case 1: /*The user didn't accept to provide the location */ break;
+                case 2: alert("The browser was unable to determine your location: " + error.message); break;
+                case 3: alert("The browser timed out before retrieving the location."); break;
+            }
         }
-
     },
     getPositionInformation: function(separator) {
         if (this.currentPosition) {



More information about the Commits mailing list