Adding a navigation history control

Probably the most commonly used control in our mapping applications will be the Navigation control. OpenLayers.Control.Navigation control integrates (makes use of) some other controls, such as OpenLayers.Control.DragPan, OpenLayers.Control.ZoomBox, or a wheel handler, which allows us to pan and zoom the map.

While navigating, moving, or zooming, it can be interesting to store a history of the navigation actions made by the user, so he/she can go back or forward to previous places. Fortunately, we don't need to reinvent the wheel. OpenLayers offers us the OpenLayers.Control.NavigationHistory control.

This recipe shows how easy it is to add it to our applications and benefit from its features.

Adding a navigation history control

As you can see in the screenshot, we are going to add a button above the map that will enable or disable the Navigation component.

How to do it...

  1. Create an HTML file with the required OpenLayers dependencies. Add the code for the toggle button that will enable/disable the navigation control:
    <button data-dojo-type="dijit.form.ToggleButton" data-dojo-props="iconClass:'dijitCheckBoxIcon', checked: true, onChange: navigationChanged">Navigation</button>
  2. Next, add a div element to hold the map:
    <div id="ch05_nav_history" style="width: 100%; height: 90%;"></div>
  3. Now, create the map instance and add a base layer:
    <script type="text/javascript">
        // Create map
        var map = new OpenLayers.Map("ch05_nav_history", {
            controls: []
        });    
        var osm = new OpenLayers.Layer.OSM();        
        map.addLayer(osm);
  4. Add the Navigation and NavigationHistory controls:
        // Add controls
        var navigation = new OpenLayers.Control.Navigation();
        var history = new OpenLayers.Control.NavigationHistory();
        var panel = new OpenLayers.Control.Panel();
        panel.addControls([history.next, history.previous]);
        
        map.addControls([navigation, history, panel]);
        map.setCenter(new OpenLayers.LonLat(0, 0), 4);
  5. Implement the function responsible to enable/disable the navigation control:
        function navigationChanged(checked) {
            if(checked) {
                navigation.activate();
            } else {
                navigation.deactivate();
            }
        }
    </script>

How it works...

First let's talk about the navigation control. Using it is not a mystery. Simply create a control instance and add it to the map:

    var navigation = new OpenLayers.Control.Navigation();
    ….
    ….
    map.addControls([navigation, ...]);

The button created at the beginning makes use of the Dojo Toolkit, which allows us to easily convert it to a toggle button. In addition, we have added a listener function to check when the button's state changes between checked and unchecked. The navigationChanged function activates or deactivates the control depending on the checked value:

    function navigationChanged(checked) {
        if(checked) {
            navigation.activate();
        } else {
            navigation.deactivate();
        }
    }

Each control has an activate() and deactivate() method. They are defined in the base class, OpenLayers.Control, and all concrete controls inherit or override these methods.

The use of activate and deactivate is preferred over removing and adding the control from/to the map. This way, there is no need to either create or attach instances of the control. The control is simply in standby until we activate it again.

That is all related to the navigation control, let's take a look at how to add the navigation history control, because this is just a two-step process.

The OpenLayers.Control.NavigationHistory control is a bit more special. It contains stacks to store the previous and next visited places and, among others, also contains references to two buttons (instances of the OpenLayers.Control.Button control class), which allows us to go back and forward in the navigation history. The references to these buttons can be found in the previous and next properties.

By default, after adding a NavigationHistory control to the map, no button appears. It is our responsibility to show the previous and next buttons on the map. For this, and other similar purposes, OpenLayers offers us the OpenLayers.Control.Panel control class. It is a special kind of control that can contain or group together other controls. So, with all this in mind, we can now explain the way the Navigation History control is added to the map.

First we need to create the OpenLayers.Control.NavigationHistory instance and add it to the map. Second, we need to add a panel to show the two buttons and add the two buttons:

    var history = new OpenLayers.Control.NavigationHistory();
    var panel = new OpenLayers.Control.Panel();
    panel.addControls([history.next, history.previous]);

Finally, the panel itself must be added to the map as a new control:

    map.addControls([navigation, history, panel]);

As you can see, we have added the navigation, the navigation history, and the panel with the buttons as map controls, simply because all three are controls.

In Chapter 6, Theming, we will see how we can change the icons used by this control.

See also

  • The Adding and removing controls recipe
  • The Placing controls outside the map recipe
  • The Understanding how themes work using the theme folder recipe in Chapter 6, Theming
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.221.144