Map Tool Tutorial
Map Tool Tutorial
Okay, so now you know how to add a map to a web page and load some data into it. Your users
can drag and zoom to their hearts’ content. You even followed the Layer Tree Tutorial so they
could switch between different datasets. (You did follow that tutorial, right?) But now you want
to do more than just view some pretty pictures. You want to let your users analyze data, or get
more info about particular features on your map, or just draw things. Basically, you want to give
them some tools.
Note
This tutorial makes heavy use of the OpenLayers mapping library. If you’re not familiar with it,
you might want to take a look at the Primer: OpenLayers before moving forward.
OpenLayers Controls
In OpenLayers, these tools for interacting with a map are called Controls. For the purposes of
this tutorial, we’ll just stick to the Measure control, a handy little tool that lets you draw a line on
the map and tells you its length in real-world units.
See also
ExtJS Buttons
While OpenLayers Controls provide a number of excellent ways of interacting with maps, they
have only limited support for actually manipulating the controls; ie, choosing which tool to use
and providing user feedback about which tool is active. ExtJS provides a richer array of options
for managing tools. Here is the idiomatic way to create an Ext.Button which activates and
deactivates an OpenLayers Control, and stays depressed while the control is active:
mapPanel.map.addControl(control);
var button = new Ext.Button({
text: 'Measure Things',
enableToggle: true,
handler: function(toggled){
if (toggled) {
control.activate();
} else {
control.deactivate();
}
}
});
The Button can be added to an ExtJS toolbar or to a panel, in whatever layout we choose. For
example, you could add the button to a MapPanel‘s top toolbar:
mapPanel.getTopToolbar().addButton(button);
mapPanel.map.addControl(length);
mapPanel.map.addControl(area);
All right, you’ve got all you need to add and activate tools to help users get the most out of your
maps.