From 74f446948f4b437070c2f85a4d9b4c3b8eb36203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Wed, 26 Jun 2013 16:22:07 +0200 Subject: [PATCH] Rotate to north for custom controls example --- examples/custom-controls.html | 22 +++++-------- examples/custom-controls.js | 59 ++++++++++------------------------- 2 files changed, 23 insertions(+), 58 deletions(-) diff --git a/examples/custom-controls.html b/examples/custom-controls.html index 3824e07d13..6d4db42f59 100644 --- a/examples/custom-controls.html +++ b/examples/custom-controls.html @@ -8,7 +8,7 @@ ol3 custom controls example @@ -73,10 +66,9 @@

This example shows how to create custom controls.

- This example creates a "zoom to extent" button. + This example creates a "rotate to north" button. See the custom-controls.js source to see how this is done. - Per default, the zoom extent control use the map projection extent.

diff --git a/examples/custom-controls.js b/examples/custom-controls.js index 9ddadcc92d..d5d774fbc7 100644 --- a/examples/custom-controls.js +++ b/examples/custom-controls.js @@ -16,7 +16,7 @@ var app = window.app; // -// Define zoom extent control. +// Define rotate to north control. // @@ -26,25 +26,26 @@ var app = window.app; * @extends {ol.control.Control} * @param {Object=} opt_options Control options. */ -app.ZoomExtentControl = function(opt_options) { +app.RotateNorthControl = function(opt_options) { var options = opt_options || {}; - this.extent_ = options.extent; var anchor = document.createElement('a'); - anchor.href = '#zoom-to'; - anchor.className = 'zoom-to'; + anchor.href = '#rotate-north'; + anchor.innerHTML = 'N'; var this_ = this; - var handleZoomTo = function(e) { - this_.handleZoomTo(e); + var handleRotateNorth = function(e) { + // prevent #rotate-north anchor from getting appended to the url + e.preventDefault(); + this_.getMap().getView().getView2D().setRotation(0); }; - anchor.addEventListener('click', handleZoomTo, false); - anchor.addEventListener('touchstart', handleZoomTo, false); + anchor.addEventListener('click', handleRotateNorth, false); + anchor.addEventListener('touchstart', handleRotateNorth, false); var element = document.createElement('div'); - element.className = 'zoom-extent ol-unselectable'; + element.className = 'rotate-north ol-unselectable'; element.appendChild(anchor); ol.control.Control.call(this, { @@ -54,46 +55,17 @@ app.ZoomExtentControl = function(opt_options) { }); }; -ol.inherits(app.ZoomExtentControl, ol.control.Control); - - -/** - * @param {Event} e Browser event. - */ -app.ZoomExtentControl.prototype.handleZoomTo = function(e) { - // prevent #zoomTo anchor from getting appended to the url - e.preventDefault(); - - var map = this.getMap(); - var view = map.getView(); - view.fitExtent(this.extent_, map.getSize()); -}; - - -/** - * Overload setMap to use the view projection's validity extent - * if no extent was passed to the constructor. - * @param {ol.Map} map Map. - */ -app.ZoomExtentControl.prototype.setMap = function(map) { - ol.control.Control.prototype.setMap.call(this, map); - if (map && !this.extent_) { - this.extent_ = map.getView().getProjection().getExtent(); - } -}; +ol.inherits(app.RotateNorthControl, ol.control.Control); // -// Create map, giving it a zoom extent control. +// Create map, giving it a rotate to north control. // var map = new ol.Map({ controls: ol.control.defaults({}, [ - new app.ZoomExtentControl({ - extent: [813079.7791264898, 848966.9639063801, - 5929220.284081122, 5936863.986909639] - }) + new app.RotateNorthControl() ]), layers: [ new ol.layer.TileLayer({ @@ -104,6 +76,7 @@ var map = new ol.Map({ target: 'map', view: new ol.View2D({ center: [0, 0], - zoom: 2 + zoom: 2, + rotation: 1 }) });