Files
openlayers/examples/custom-controls.js
Frederic Junod 79c8afdba8 Simplify import path in examples
To have the same path (starting with `ol/`, without `.js`) as in the documentation.
The support was added in the webpack config in #8928
2018-11-26 17:18:52 +01:00

66 lines
1.2 KiB
JavaScript

import Map from 'ol/Map';
import View from 'ol/View';
import {defaults as defaultControls, Control} from 'ol/control';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
//
// Define rotate to north control.
//
/**
* @constructor
* @extends {module:ol/control/Control~Control}
* @param {Object=} opt_options Control options.
*/
class RotateNorthControl extends Control {
constructor(opt_options) {
const options = opt_options || {};
const button = document.createElement('button');
button.innerHTML = 'N';
const element = document.createElement('div');
element.className = 'rotate-north ol-unselectable ol-control';
element.appendChild(button);
super({
element: element,
target: options.target
});
button.addEventListener('click', this.handleRotateNorth.bind(this), false);
}
handleRotateNorth() {
this.getMap().getView().setRotation(0);
}
}
//
// Create map, giving it a rotate to north control.
//
const map = new Map({
controls: defaultControls().extend([
new RotateNorthControl()
]),
layers: [
new TileLayer({
source: new OSM()
})
],
target: 'map',
view: new View({
center: [0, 0],
zoom: 3,
rotation: 1
})
});