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

commits at geoext.org commits at geoext.org
Sun Jan 16 06:14:26 CET 2011


Author: cmoullet
Date: 2011-01-16 06:14:26 +0100 (Sun, 16 Jan 2011)
New Revision: 2564

Modified:
   sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapimobileexample.html
   sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapimobileexample.js
   sandbox/cmoullet/ux/GeolocationAPI/lib/GeoExt.ux/control/GeolocationAPI.js
Log:
Add documentation

Modified: sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapimobileexample.html
===================================================================
--- sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapimobileexample.html	2011-01-14 16:09:31 UTC (rev 2563)
+++ sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapimobileexample.html	2011-01-16 05:14:26 UTC (rev 2564)
@@ -22,7 +22,7 @@
             position: absolute;
             right: 10px;
             top: 10px;
-            width: 200px;
+            width: 250px;
             z-index: 1000;
             background: white;
             padding-left: 5px;
@@ -32,7 +32,6 @@
         }
     </style>
 
-
     <!-- <script type="text/javascript"
             src="http://c2c-rd-geospatial.demo-camptocamp.com/lib/openlayers/OpenLayers.mobile.min.js"></script>-->
     <script type="text/javascript"

Modified: sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapimobileexample.js
===================================================================
--- sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapimobileexample.js	2011-01-14 16:09:31 UTC (rev 2563)
+++ sandbox/cmoullet/ux/GeolocationAPI/examples/geolocationapimobileexample.js	2011-01-16 05:14:26 UTC (rev 2564)
@@ -20,7 +20,6 @@
     // enable delayed tile loading for better performance
     layer.tileLoadingDelay = 300;
 
-
     map.addLayers([layer]);
 
     map.zoomToMaxExtent();

Modified: sandbox/cmoullet/ux/GeolocationAPI/lib/GeoExt.ux/control/GeolocationAPI.js
===================================================================
--- sandbox/cmoullet/ux/GeolocationAPI/lib/GeoExt.ux/control/GeolocationAPI.js	2011-01-14 16:09:31 UTC (rev 2563)
+++ sandbox/cmoullet/ux/GeolocationAPI/lib/GeoExt.ux/control/GeolocationAPI.js	2011-01-16 05:14:26 UTC (rev 2564)
@@ -1,11 +1,19 @@
 /**
  * @requires OpenLayers/Control.js
+ * @include OpenLayers/Layer/Vector.js
+ * @include OpenLayers/Geometry/Point.js
+ * @include OpenLayers/Feature/Vector.js
+ * @include OpenLayers/BaseTypes/Bounds.js
+ * @include OpenLayers/BaseTypes/LonLat.js
+ * @include OpenLayers/Projection.js
  */
 
 /**
  * Class: OpenLayers.Control.GeolocationAPI
  *
+ * Usage of the HTML5 Geolocation API specification http://dev.w3.org/geo/api/spec-source.html
  * For wider support, http://code.google.com/p/geo-location-javascript/ could be useful
+ * Requires proj4js support
  *
  * Inherits from:
  *  - <OpenLayers.Control>
@@ -13,40 +21,123 @@
 
 OpenLayers.Control.GeolocationAPI = OpenLayers.Class(OpenLayers.Control, {
 
-    isGeolocationAPISupported: null,
-
+    /**
+     * APIProperty: mode
+     * {<String>}
+     *    Define the usage of the Geolocation API.
+     *    Possible values: "position" or "tracking"
+     *    Default value: "position"
+     */
     mode: "position",
 
+    /**
+     * APIProperty: zoom
+     * {<Integer>}
+     *    Zoom level used to recenter the map. If displayAccuracy is set to true, then the zoom extent is defined by accuracy and not by the zoom property
+     *    Default value: 12
+     */
     zoom: 12,
 
-    currentPosition: null,
-
-    currentPositionMapCoordinate: null,
-
+    /**
+     * APIProperty: geolocationOptions
+     * {<Object>}
+     *    The geolocation API spec defines three optional parameters used for the position determination:
+     *    - enableHighAccuracy (boolean, default false): provides a hint that the application would like to receive the best possible results.
+     *    - timeout (long, default 0): denotes the maximum length of time (expressed in milliseconds) that is allowed to pass from the call to getCurrentPosition() or watchPosition() until the corresponding successCallback is invoked.
+     *    - maximumAge (long, default Infinity): indicates that the application is willing to accept a cached position whose age is no greater than the specified time in milliseconds.
+     *    Default value: {}
+     */
     geolocationOptions: {},
 
-    trackingId: false,
-
+    /**
+     * APIProperty: errorAlert
+     * {<Boolean>}
+     *    Provide error alerts when geolocation API encounters error
+     *    Default value: false
+     */
     errorAlert: false,
 
+    /**
+     * APIProperty: vector
+     * {<OpenLayers.Layer.Vector>}
+     *    Vector layer used to display the position and the accuracy. A layer is created if the accuracy or the position needs to be represented and if none is provided.
+     */
+    vector: null,
+
+    /**
+     * APIProperty: displayPosition
+     * {<Boolean>}
+     *    Display the position as point in a vector layer
+     *    Default value: false
+     */
     displayPosition: false,
 
+    /**
+     * APIProperty: displayAccuracy
+     * {<Boolean>}
+     *    Display the accuracy as circle in a vector layer
+     *    Default value: false
+     */
     displayAccuracy: false,
 
+    /**
+     * Parameter: currentPosition
+     * {<Position>}
+     *    Current position determined by the Geolocation API
+     */
+    currentPosition: null,
+
+    /**
+     * Parameter: currentPositionMapCoordinate
+     * {<OpenLayers.LonLat>}
+     *    Current position in lon/lat of the map coordinate system
+     */
+    currentPositionMapCoordinate: null,
+
+    /**
+     * Parameter: isGeolocationAPISupported
+     * {<Boolean>}
+     *    Informs if the Geolocation API is supported
+     */
+    isGeolocationAPISupported: null,
+
+    /**
+     * Parameter: trackingId
+     * {<String>}
+     *    Id of the tracking made by the Geolocation API
+     */
+    trackingId: false,
+
+    /**
+     * Parameter: positionFeature
+     * {<OpenLayers.Feature.Vector>}
+     *    Feature representing the position
+     */
     positionFeature: null,
 
+    /**
+     * Parameter: accuracyFeature
+     * {<OpenLayers.Feature.Vector>}
+     *    Feature representing the accuracy
+     */
     accuracyFeature: null,
 
     /**
      * Constant: EVENT_TYPES
      *
      * Supported event types:
-     * positioncomputed - Triggered when position has been computed
-     * positionerror - Triggered when the geolocation API return an error
-     * geolocationapinotsupported - Triggered when the Geolocation API is not suppported
+     * - positioncomputed - Triggered when position has been computed
+     * - positionerror - Triggered when the geolocation API return an error
+     * - geolocationapinotsupported - Triggered when the Geolocation API is not suppported
      */
     EVENT_TYPES: ["positioncomputed", "positionerror", "geolocationapinotsupported"],
 
+    /**
+     * Constructor: OpenLayers.Control.GeolocationAPI
+     *
+     * Parameters:
+     * options - {Object}
+     */
     initialize: function(options) {
         options = options || {};
 
@@ -56,8 +147,7 @@
                 );
         if (!options.vector && (options.displayPosition || options.displayAccuracy)) {
             options.vector = new OpenLayers.Layer.Vector("GeolocationAPIVector",
-            {displayInLayerSwitcher: false}
-                    );
+            {displayInLayerSwitcher: false});
         }
 
         OpenLayers.Util.applyDefaults(this, options);
@@ -72,8 +162,15 @@
         }
     },
 
+    /**
+     * APIMethod: activate
+     * Activate the control and activate the usage of the Geolocation API.
+     * Adds the vector layer, if needed
+     *
+     * Returns:
+     * {Boolean} Successfully activated the control.
+     */
     activate: function () {
-
         if (this.isGeolocationAPISupported && this.mode === "position") {
             this.activatePosition();
         }
@@ -83,19 +180,45 @@
         if (this.displayPosition || this.displayAccuracy) {
             this.map.addLayers([this.vector]);
         }
-        OpenLayers.Control.prototype.activate.apply(this);
+        return OpenLayers.Control.prototype.activate.apply(this);
     },
+
+    /**
+     * APIMethod: deactivate
+     * Deactivate the control and all handlers.
+     * Stop the tracking and remove the displayed features
+     *
+     * Returns:
+     * {Boolean} Successfully deactivated the control.
+     */
     deactivate: function () {
-        if (this.trackingId) {
-            navigator.geolocation.clearWatch(this.trackingId);
-            this.trackingId = false;
-        }
+        this.stopTracking();
         if (this.displayPosition || this.displayAccuracy) {
             this.removeFeatures();
             this.map.removeLayer(this.vector);
         }
-        OpenLayers.Control.prototype.deactivate.apply(this);
+        return OpenLayers.Control.prototype.deactivate.apply(this);
     },
+
+    /**
+     * APIMethod: stopTracking
+     * Stop the tracking
+     *
+     * Returns:
+     * {Boolean} Successfully deactivated the control.
+     */
+    stopTracking: function() {
+        if (this.trackingId) {
+            navigator.geolocation.clearWatch(this.trackingId);
+            this.trackingId = false;
+        }
+    },
+
+    /**
+     * APIMethod: removeFeatures
+     * Remove the position and accuracy features
+     *
+     */
     removeFeatures: function() {
         if (this.positionFeature) {
             this.vector.removeFeatures([this.positionFeature]);
@@ -104,6 +227,13 @@
             this.vector.removeFeatures([this.positionFeature]);
         }
     },
+
+    /**
+     * APIMethod: showFeatures
+     * Show the position and accuracy features.
+     * If accuracy feature is display, a zoom to its extent is done
+     *
+     */
     showFeatures: function() {
         var point = new OpenLayers.Geometry.Point(this.currentPositionMapCoordinate.lon,
                 this.currentPositionMapCoordinate.lat);
@@ -123,13 +253,31 @@
             this.map.zoomToExtent(bbox);
         }
     },
+
+    /**
+     * APIMethod: activatePosition
+     * Use the getCurrentPosition function of the Geolocation API to determine its position
+     *
+     */
     activatePosition: function() {
-        navigator.geolocation.getCurrentPosition(OpenLayers.Function.bind(this.centerMap, this), OpenLayers.Function.bind(this.locationError, this), this.geolocationOptions);
+        navigator.geolocation.getCurrentPosition(OpenLayers.Function.bind(this._successCallback, this), OpenLayers.Function.bind(this._errorCallback, this), this.geolocationOptions);
     },
+
+    /**
+     * APIMethod: activateTracking
+     * Use the watchPosition function of the Geolocation API to track its position
+     *
+     */
     activateTracking: function() {
-        this.trackingId = navigator.geolocation.watchPosition(OpenLayers.Function.bind(this.centerMap, this), OpenLayers.Function.bind(this.locationError, this), this.geolocationOptions);
+        this.trackingId = navigator.geolocation.watchPosition(OpenLayers.Function.bind(this._successCallback, this), OpenLayers.Function.bind(this._errorCallback, this), this.geolocationOptions);
     },
-    centerMap: function(position) {
+
+    /**
+     * Method: _successCallback
+     * Success callback function. Center the map and shows the features
+     * positioncomputed event is triggered
+     */
+    _successCallback: function(position) {
         this.currentPosition = position;
         var lonLat = new OpenLayers.LonLat(position.coords.longitude, position.coords.latitude);
         this.currentPositionMapCoordinate = lonLat.transform(new OpenLayers.Projection("EPSG:4326"),
@@ -140,17 +288,33 @@
         this.events.triggerEvent("positioncomputed", {position : position});
     },
 
-    locationError: function(error) {
+    /**
+     * Method: _errorCallback
+     * Error callback function. Send alerts if required
+     * positionerror event is triggered
+     */
+    _errorCallback: function(error) {
         this.events.triggerEvent("positionerror", {error : error});
         if (this.errorAlert) {
             switch (error.code) {
                 case 0: alert(OpenLayers.i18n("There was an error while retrieving your location: ") + error.message); break;
-                case 1: /*The user didn't accept to provide the location */ break;
+                case 1: alert(OpenLayers.i18n("The user didn't accept to provide the location: ")); break;
                 case 2: alert(OpenLayers.i18n("The browser was unable to determine your location: ") + error.message); break;
                 case 3: alert(OpenLayers.i18n("The browser timed out before retrieving the location.")); break;
             }
         }
     },
+
+    /**
+     * APIMethod: getPositionInformation
+     * Get textual position information
+     *
+     * Parameters:
+     * separator - {<String>} a separator between the position information. Can be "<BR>", for example.
+     *
+     * Returns:
+     * {String} All position information provided by the Geolocation API.
+     */
     getPositionInformation: function(separator) {
         if (this.currentPosition) {
             var positionString = OpenLayers.i18n("Longitude: ") + this.currentPosition.coords.longitude + separator;
@@ -160,9 +324,41 @@
             positionString = positionString + OpenLayers.i18n("Altitude Accuracy: ") + this.currentPosition.coords.altitudeAccuracy + separator;
             positionString = positionString + OpenLayers.i18n("Heading: ") + this.currentPosition.coords.heading + separator;
             positionString = positionString + OpenLayers.i18n("Speed: ") + this.currentPosition.coords.speed + separator;
+            positionString = positionString + OpenLayers.i18n("Date: ") + this.formatDate(new Date(this.currentPosition.timestamp), '%Y/%M/%d') + separator;
+            positionString = positionString + OpenLayers.i18n("Time (UTC): ") + this.formatDate(new Date(this.currentPosition.timestamp), '%H:%m:%s') + separator;
             return positionString;
         } else {
             return OpenLayers.i18n("Not available");
         }
+    },
+
+    /**
+     * Method: formatDate
+     * Format the date in UTC time
+     * TODO: shouldn't be in this control
+     */
+    formatDate: function(date, fmt) {
+        function pad(value) {
+            return (value.toString().length < 2) ? '0' + value : value;
+        }
+
+        return fmt.replace(/%([a-zA-Z])/g, function (_, fmtCode) {
+            switch (fmtCode) {
+                case 'Y':
+                    return date.getUTCFullYear();
+                case 'M':
+                    return pad(date.getUTCMonth() + 1);
+                case 'd':
+                    return pad(date.getUTCDate());
+                case 'H':
+                    return pad(date.getUTCHours());
+                case 'm':
+                    return pad(date.getUTCMinutes());
+                case 's':
+                    return pad(date.getUTCSeconds());
+                default:
+                    throw new Error('Unsupported format code: ' + fmtCode);
+            }
+        });
     }
 });
\ No newline at end of file



More information about the Commits mailing list