On the official openlayers site there is a custom control example which demonstration a class base control.
Can anyone give an example of a simple custom control without class usage?
According to the ol.control.Control specification, it should be possible to create one using the following statement
var myControl = new ol.control.Control({element: myElement});
but I'm quite new to javascript and cannot figure out yet how to use it.
Answer
// Preparation for the custom control:
var anchor_element = document.createElement('a');
anchor_element.href = '#custom_href';
anchor_element.className = 'custom_anchor';
var this_ = this;
var handleCustomControl = function (e) {
myControl.customFunction(e);
};
anchor_element.addEventListener('click', handleCustomControl, false);
anchor_element.addEventListener('touchstart', handleCustomControl, false);
var custom_element = document.createElement('div');
custom_element.className = 'myCustomControl ol-unselectable';
custom_element.appendChild(anchor_element);
myControl = new ol.control.Control({
className: 'myControl',
element: custom_element,
target: document.getElementById("myCustomControl")
});
CSS-rule (to be able to click on the hyperlink):
.custom_anchor:before {
content:"Cycle";
}
I gave it a quick try and can use a custom Control: http://jsfiddle.net/expedio/onn6g33c/
Probably it's a better choice to set up a control like this if it's getting a bit more complex: http://jsfiddle.net/expedio/j4duqc9s/
No comments:
Post a Comment