UNPKG

1.59 kBJavaScriptView Raw
1goog.require('ol');
2goog.require('ol.Map');
3goog.require('ol.View');
4goog.require('ol.control');
5goog.require('ol.control.Control');
6goog.require('ol.layer.Tile');
7goog.require('ol.source.OSM');
8
9
10/**
11 * Define a namespace for the application.
12 */
13window.app = {};
14var app = window.app;
15
16
17//
18// Define rotate to north control.
19//
20
21
22/**
23 * @constructor
24 * @extends {ol.control.Control}
25 * @param {Object=} opt_options Control options.
26 */
27app.RotateNorthControl = function(opt_options) {
28
29 var options = opt_options || {};
30
31 var button = document.createElement('button');
32 button.innerHTML = 'N';
33
34 var this_ = this;
35 var handleRotateNorth = function() {
36 this_.getMap().getView().setRotation(0);
37 };
38
39 button.addEventListener('click', handleRotateNorth, false);
40 button.addEventListener('touchstart', handleRotateNorth, false);
41
42 var element = document.createElement('div');
43 element.className = 'rotate-north ol-unselectable ol-control';
44 element.appendChild(button);
45
46 ol.control.Control.call(this, {
47 element: element,
48 target: options.target
49 });
50
51};
52ol.inherits(app.RotateNorthControl, ol.control.Control);
53
54
55//
56// Create map, giving it a rotate to north control.
57//
58
59
60var map = new ol.Map({
61 controls: ol.control.defaults({
62 attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
63 collapsible: false
64 })
65 }).extend([
66 new app.RotateNorthControl()
67 ]),
68 layers: [
69 new ol.layer.Tile({
70 source: new ol.source.OSM()
71 })
72 ],
73 target: 'map',
74 view: new ol.View({
75 center: [0, 0],
76 zoom: 3,
77 rotation: 1
78 })
79});