The penultimate control that we add to our application before we consider it finished, is the rotation control in the next example: ch06_rotate. We will place it in the bottom bar along with the upcoming projection control. In the first step, we extend our HTML file with container elements for them:
<div class="notification-bar">
<div id="messageBar" class="message-bar"></div>
<div id="projection"></div>
<div id="rotation"></div>
<div id="coordinates"></div>
</div>Next, we create some CSS rules for the rotation control:
.notification-bar #rotation {
width: 10%;
text-align: center;
}
.notification-bar #rotation input {
text-align: center;
outline: none;
border: 0;
-moz-appearance: textfield;
}We align the control to the center of the container element and remove any border or outline from it.
Next, we create a simple control to change the map's rotation every time our numeric input changes:
ol.control.RotationControl = function (opt_options) {
var options = opt_options || {};
var _this = this;
var controlInput = document.createElement('input');
controlInput.title = options.tipLabel || 'Set rotation';
controlInput.type = 'number';
controlInput.min = 0;
controlInput.max = 360;
controlInput.step = 1;
controlInput.value = 0;
controlInput.addEventListener('change', function (evt) {
var radianValue = this.value / 180 * Math.PI;
_this.getMap().getView().setRotation(radianValue);
});
ol.control.Control.call(this, {
element: controlInput,
target: options.target
});
this.set('element', controlInput);
};
ol.inherits(ol.control.RotationControl, ol.control.Control);The only thing we have to make sure here is that we convert the input values from degrees to radians, as OpenLayers 3 uses radian values. We also make sure that our input updates if the map's rotation changes from other sources (for example, from the navigation history):
ol.control.RotationControl.prototype.setMap = function (map) {
ol.control.Control.prototype.setMap.call(this, map);
if (map === null) {
ol.Observable.unByKey(this.get('eventId'));
} else {
this.set('eventId', map.getView().on('change:rotation', function (evt) {
var degreeValue = Math.round(map.getView().getRotation() / Math.PI * 180);
this.get('element').value = degreeValue;
}, this));
}
};Finally, we add the control to the map with our destination element as the target:
var map = new ol.Map({
[…]
controls: [
[…]
new ol.control.RotationControl({
target: 'rotation'
})
[…]
});Now, you can try out our new control, and rotate the map between 0 degrees and 360 degrees:
