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

commits at geoext.org commits at geoext.org
Mon Jan 10 17:28:10 CET 2011


Author: cmoullet
Date: 2011-01-10 17:28:10 +0100 (Mon, 10 Jan 2011)
New Revision: 2550

Modified:
   sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.html
   sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.js
   sandbox/cmoullet/ux/GeolocationAPI/lib/GeoExt.ux/control/GeolocationAPI.js
Log:
Add positioncomputed event and provide position feedback

Modified: sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.html
===================================================================
--- sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.html	2011-01-10 15:35:29 UTC (rev 2549)
+++ sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.html	2011-01-10 16:28:10 UTC (rev 2550)
@@ -22,5 +22,6 @@
 <p style="margin-bottom:15px;">See <a href="geolocationapiexample.js">geolocationapiexample.js</a> for the source code.</p>
 
 <div id="mapppanel"></div>
+<div id="positionInfo"></div>
 </body>
 </html>
\ No newline at end of file

Modified: sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.js
===================================================================
--- sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.js	2011-01-10 15:35:29 UTC (rev 2549)
+++ sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapiexample.js	2011-01-10 16:28:10 UTC (rev 2550)
@@ -4,7 +4,7 @@
     Ext.QuickTips.init();
 
     var map = new OpenLayers.Map();
-    var GeolocationAPIControl = new OpenLayers.Control.GeolocationAPI({});
+    var GeolocationAPIControl = new OpenLayers.Control.GeolocationAPI({mode: 'position', zoom: 11});
     map.addControl(GeolocationAPIControl);
 
     var mapPanel = new GeoExt.MapPanel({
@@ -18,5 +18,10 @@
         center: [7.5, 47],
         zoom: 8
     });
+
+    GeolocationAPIControl.events.register("positioncomputed", GeolocationAPIControl, function(position) {
+        document.getElementById("positionInfo").innerHTML = this.getPositionInformation("<BR>");
+    });
     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-10 15:35:29 UTC (rev 2549)
+++ sandbox/cmoullet/ux/GeolocationAPI/lib/GeoExt.ux/control/GeolocationAPI.js	2011-01-10 16:28:10 UTC (rev 2550)
@@ -19,9 +19,26 @@
 
     currentPosition: null,
 
+    geolocationOptions: {},
+
+    /**
+     * Constant: EVENT_TYPES
+     *
+     * Supported event types:
+     * positioncomputed - Triggered when position has been computed
+     */
+    EVENT_TYPES: ["positioncomputed"],
+
     initialize: function(options) {
         options = options || {};
 
+        this.EVENT_TYPES =
+        OpenLayers.Control.GeolocationAPI.prototype.EVENT_TYPES.concat(
+                OpenLayers.Control.prototype.EVENT_TYPES
+                );
+
+        OpenLayers.Util.applyDefaults(this, options);
+
         if (navigator.geolocation) {
             this.isGeolocationAPISupported = true;
         } else {
@@ -41,7 +58,7 @@
         OpenLayers.Control.prototype.deactivate.apply(this);
     },
     activatePosition: function() {
-        navigator.geolocation.getCurrentPosition(this.centerMap.bind(this), this.locationError);
+        navigator.geolocation.getCurrentPosition(this.centerMap.bind(this), this.locationError, this.geolocationOptions);
     },
     centerMap: function(position) {
         this.currentPosition = position;
@@ -49,9 +66,30 @@
         var mapCoordinate = lonLat.transform(new OpenLayers.Projection("EPSG:4326"),
                 this.map.getProjectionObject());
         this.map.setCenter(mapCoordinate, this.zoom);
+        this.events.triggerEvent("positioncomputed", {position : position});
     },
+
     locationError: function(error) {
-        alert("Error with GeoLocation API: " + error.code);
+        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) {
+            var positionString = "Longitude: " + this.currentPosition.coords.longitude + separator;
+            positionString = positionString + "Latitude: " + this.currentPosition.coords.latitude + separator;
+            positionString = positionString + "Accuracy: " + this.currentPosition.coords.accuracy + separator;
+            positionString = positionString + "Altitude: " + this.currentPosition.coords.altitude + separator;
+            positionString = positionString + "Altitude Accuracy: " + this.currentPosition.coords.altitudeAccuracy + separator;
+            positionString = positionString + "Heading: " + this.currentPosition.coords.heading + separator;
+            positionString = positionString + "Speed: " + this.currentPosition.coords.speed + separator;
+            return positionString;
+        } else {
+            return "Not available";
+        }
     }
 });
\ No newline at end of file



More information about the Commits mailing list