Are you looking for giving your OpenLayers map controls a cool appearance, smoothly integrated with the site’s theme, without writing a papyrus and scatter code among lot of files?
Then have a look at jQuery UI CSS framework, a system of classes developed for jQuery UI widgets.
This is the map toolbar of the webGIS site we are busy developing, rendered with UI-Darkness theme:

The controls (pan, measure and zoom) are OpenLayers’ controls. They are all created in the map’s init() (see first js snippet below). The binding with the buttons is made by name – therefore be sure that the names of the OpenLayers controls match exactly the name properties of the respective buttons. The activation of the selected control is done by the toggleControl() function, further below in the js snippet. That way you can add as many control-button pairs as you need.
Let’s see what the code looks like. It is not so much indeed.. My tribute to the proverbial programmer’s laziness and to the koan of Master Foo’s and the Ten Thousand Lines.
First be sure to include all necessary scripts in the html head:
<link type="text/css" href="/static/css/jquery.fancybox-1.2.6.css" rel="stylesheet" media="screen" />
<script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/static/js/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript" src="/static/js/jquery.fancybox-1.2.6.pack.js"></script>
<script type="text/javascript" src="/static/js/jquery.easing.1.3.js"></script>
then add the buttons – I took inspiration from Filament Group excellent post:
<div id="mapcontrols" class="fg-buttonset fg-buttonset-single ui-helper-clearfix">
<button name='navigate'class="fg-button ui-state-default ui-state-active ui-priority-primary ui-corner-left" >Navigate</button>
<button name='line' class="fg-button ui-state-default ui-priority-primary">Measure line</button>
<button name='polygon' class="fg-button ui-state-default ui-priority-primary">Measure area</button>
<a href="#" name='zoomin' class="fg-button ui-state-default fg-button-icon-solo" title="Zoom in"><span class="ui-icon ui-icon-circle-zoomin"></span> Zoom in</a>
<a href="#" name='zoomout' class="fg-button ui-state-default fg-button-icon-solo ui-corner-right" title="Zoom out"><span class="ui-icon ui-icon-circle-zoomout"></span> Zoom out</a>
</div>
And in the end the JavaScript – add the following snippet in the map’s init() function:
mapControls = {
line: new OpenLayers.Control.Measure(
OpenLayers.Handler.Path, {
persist: true
}
),
polygon: new OpenLayers.Control.Measure(
OpenLayers.Handler.Polygon, {
persist: true
}
),
zoomin: new OpenLayers.Control.ZoomBox(
{title:"Zoom in box", out: false}
),
zoomout: new OpenLayers.Control.ZoomBox(
{title:"Zoom out box", out: true}
)
};
var control;
for(var key in mapControls) {
control = mapControls[key];
control.events.on({
"measure": handleMeasurements,
"measurepartial": handleMeasurements
});
map.addControl(control);
}
and these functions at the bottom of your js file:
function handleMeasurements(event) {
var geometry = event.geometry;
var units = event.units;
var order = event.order;
var measure = event.measure;
var element = document.getElementById('output'); //TODO redirect to other area?
var out = "";
if(order == 1) {
out += "Measure: " + measure.toFixed(3) + " " + units;
} else {
out += "Measure: " + measure.toFixed(3) + " " + units + "2";
}
element.innerHTML = out;
}
function toggleControl(element) {
for(key in mapControls) {
var control = mapControls[key];
//alert ($(element).is('.ui-state-active'));
if(element.name == key && $(element).is('.ui-state-active')) {
control.activate();
} else {
control.deactivate();
}
}
}
$(function(){
//all hover and click logic for buttons
$(".fg-button:not(.ui-state-disabled)")
.hover(
function(){
$(this).addClass("ui-state-hover");
},
function(){
$(this).removeClass("ui-state-hover");
}
)
.mousedown(function(){
$(this).parents('.fg-buttonset-single:first').find\
(".fg-button.ui-state-active").removeClass("ui-state-active");
if( $(this).is('.ui-state-active.fg-button-toggleable, \
.fg-buttonset-multi .ui-state-active') )
{ $(this).removeClass("ui-state-active"); }
else { $(this).addClass("ui-state-active"); }
})
.mouseup(function(){
if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button, \
.fg-buttonset-multi .fg-button') ){
$(this).removeClass("ui-state-active");
}
//TODO use this else only for measure/pan toggle.
else {toggleControl(this);}
});
});
Ok, should be all you have to know to set up the toolbar! Feel free to reuse the code and improve it ![]()
Oh, and don’t forget to tweak the CSS to get the perfect look and feel
–anne
[...] панель инструментов для [...]
[...] – complici l’influenza che mi ha tenuto a riposo forzato e l’ispirazione tratta da Linfiniti – sono riuscito [...]
[...] a map toolbar and added some controls, which was inspired/stolen from this great post from Linfiniti. That article was written pre-jQuery UI 1.8, so it’s quite a bit easier now to [...]
Hey Anne,
that’s a great job, but you forgot to show the whole code!
king regard
[...] finalmente – complici l’influenza che mi ha tenuto a riposo forzato e l’ispirazione tratta da Linfiniti – sono riuscito [...]