Skip to content
Justin Manley edited this page Nov 1, 2017 · 4 revisions

Leaflet.toolbar makes it easy for developers to enable users to interact with Leaflet maps and layers.

Leaflet.toolbar supplies two toolbar styles out of the box: popup-style toolbars fixed to a point on the map (L.Toolbar2.Popup), and control-style toolbars fixed in relation to the map pane (L.Toolbar2.Control).

L.Toolbar2.Control

new L.Toolbar2.Control({
    position: 'topleft',
    actions: [
        MyToolbarAction1,
        MyToolbarAction2
    ]
}).addTo(map);

Control toolbars are useful for actions which alter the state of the entire map and/or actions which should be exposed to the user regardless of which region of the map is visible.

Creation

Constructor Description
L.Toolbar2.Control(<ToolbarOptions> options) Creates a control toolbar with the specified actions and options. The toolbar will appear when it is added to the map using map.addLayer or toolbar.addTo.

Methods

Method Returns Description
addTo(<Map> map, ...) this Opens the toolbar on the map. The map and any other arguments called with #addTo will be passed to the constructor for each action in the toolbar.

L.Toolbar2.Popup

shape.on('click', function(event) {
    new L.Toolbar2.Popup(event.latlng, {
        actions: [
            MyToolbarAction3,
            MyToolbarAction4
        ]
    }).addTo(map, shape);
});

Popup controls can be useful when you want to enable users to edit a feature on the map. In these cases, popup toolbars can be easier and more intuitive for users because they expose available actions directly above the target feature (rather than in the corner of a map).

Creation

Constructor Description
L.Toolbar2.Popup(<LatLng> latlng, <ToolbarOptions> options) Creates a popup toolbar with the specified actions and options. Toolbar will appear at the specified latlng when added to the map.

Methods

Method Returns Description
addTo(<Map> map, ...) this Opens the toolbar on the map. The map and any other arguments called with #addTo will be passed to the constructor for each action in the toolbar.
setLatLng(<LatLng> latlng) this Sets the latlng of the popup toolbar. If the toolbar is on the map, it will be moved. If the toolbar is not on the map, it will appear at the new latlng the next time it is added.

ToolbarOptions

Option Type Default Description
className String '' Class to be added to the <ul> element containing the toolbar's icons.
actions Action[] [] Array of actions to be exposed to the user in the toolbar.
position String topright Where in the map pane to display the control toolbar. Only applies to instances of L.Toolbar2.Control. See control positions for permissible values.

Adding and removing toolbars

Toolbars can be added to and removed from the map in much the same way as feature layers:

var toolbar = new L.Toolbar2.Control(); // empty toolbar

/* Add the toolbar to the map. */
toolbar.addTo(map); // preferred
map.addLayer(toolbar); // also an option

/* Remove the toolbar. */
map.removeLayer(toolbar);

L.Toolbar2.Action

L.Toolbar2.Action is a lot like L.Handler, except that, behind the scenes, it takes care of all the grunt work of creating an icon for your action and attaching and detaching click handlers. All actions to be exposed to the user via a Leaflet.toolbar toolbar must extend L.Toolbar2.Action. No exceptions.

Existing L.Handler actions can be wrapped in a L.Toolbar2.Action for use in a Leaflet.toolbar toolbar. See the leaflet-draw-toolbar repository for examples of this, particularly DrawAction#fromHandler and EditAction#fromHandler.

Creation

Constructor Description
L.Toolbar2.Action(<ToolbarActionOptions> options) Creates a toolbar action.

Methods

Method Returns Description
enable - Trigger the action.
disable - Stop the action.
enabled Boolean Returns true if the action is currently running.

ToolbarActionOptions

Option Type Default Description
toolbarIcon <ToolbarIconOptions> See below. Default settings for the icon to accompany the toolbar action.
subToolbar L.Toolbar new L.Toolbar() A toolbar with additional actions to be shown when the Action is enabled and hidden when it is disabled.

ToolbarIconOptions

Option Type Default Description
html String '' HTML string to be placed inside <a> element representing the toolbar action.
className String '' Class name to be applied to the <a> element representing the toolbar action.
tooltip String '' Tooltip to be shown when the cursor hovers over the icon.