Merge pull request #2019 from fredj/device-orientation-example

Add an extent constraint to the device-orientation example
This commit is contained in:
Frédéric Junod
2014-05-26 13:54:56 +02:00
2 changed files with 11 additions and 6 deletions
+5 -1
View File
@@ -3,10 +3,14 @@ goog.require('ol.Map');
goog.require('ol.View2D'); goog.require('ol.View2D');
goog.require('ol.dom.Input'); goog.require('ol.dom.Input');
goog.require('ol.layer.Tile'); goog.require('ol.layer.Tile');
goog.require('ol.proj');
goog.require('ol.source.OSM'); goog.require('ol.source.OSM');
var projection = ol.proj.get('EPSG:3857');
var view = new ol.View2D({ var view = new ol.View2D({
center: [0, 0], center: [0, 0],
projection: projection,
extent: projection.getExtent(),
zoom: 2 zoom: 2
}); });
var map = new ol.Map({ var map = new ol.Map({
@@ -41,5 +45,5 @@ deviceOrientation.on(['change:beta', 'change:gamma'], function(event) {
center[0] -= resolution * gamma * 25; center[0] -= resolution * gamma * 25;
center[1] += resolution * beta * 25; center[1] += resolution * beta * 25;
view.setCenter(center); view.setCenter(view.constrainCenter(center));
}); });
+6 -5
View File
@@ -59,14 +59,13 @@ ol.View2DProperty = {
* that is passed to a setter will effectively be the value set in the view, * that is passed to a setter will effectively be the value set in the view,
* and returned by the corresponding getter. * and returned by the corresponding getter.
* *
* But an `ol.View2D` object also has a *resolution constraint* and a * But an `ol.View2D` object also has a *resolution constraint*, a
* *rotation constraint*. There's currently no *center constraint*, but * *rotation constraint* and a *center constraint*.
* this may change in the future.
* *
* As said above no constraints are applied when the setters are used to set * As said above no constraints are applied when the setters are used to set
* new states for the view. Applying constraints is done explicitly through * new states for the view. Applying constraints is done explicitly through
* the use of the `constrain*` functions (`constrainResolution` and * the use of the `constrain*` functions (`constrainResolution` and
* `constrainRotation`). * `constrainRotation` and `constrainCenter`).
* *
* The main users of the constraints are the interactions and the * The main users of the constraints are the interactions and the
* controls. For example, double-clicking on the map changes the view to * controls. For example, double-clicking on the map changes the view to
@@ -184,8 +183,10 @@ ol.View2D.prototype.calculateCenterZoom = function(resolution, anchor) {
/** /**
* Get the constrained center of this view.
* @param {ol.Coordinate|undefined} center Center. * @param {ol.Coordinate|undefined} center Center.
* @return {ol.Coordinate|undefined} Constrained center. * @return {ol.Coordinate|undefined} Constrained center.
* @todo api
*/ */
ol.View2D.prototype.constrainCenter = function(center) { ol.View2D.prototype.constrainCenter = function(center) {
return this.constraints_.center(center); return this.constraints_.center(center);
@@ -193,7 +194,7 @@ ol.View2D.prototype.constrainCenter = function(center) {
/** /**
* Get the constrained the resolution of this view. * Get the constrained resolution of this view.
* @param {number|undefined} resolution Resolution. * @param {number|undefined} resolution Resolution.
* @param {number=} opt_delta Delta. Default is `0`. * @param {number=} opt_delta Delta. Default is `0`.
* @param {number=} opt_direction Direction. Default is `0`. * @param {number=} opt_direction Direction. Default is `0`.