We’ll start with the construction of the user interface components of the toolbar and then we’ll move on to discussing how to hook the tools into your map.
Using Dijits To Create the Toolbar and Buttons
The toolbar you see in the example is created using the Toolbar and Button user interface controls provided by Dijit (a sub-project of Dojo). Each control is enclosed within a <div> tag inside the <body> of the web page with all buttons being enclosed by the surrounding <div> that contains the Toolbar dijit.
Each button can be assigned both an icon and text. Notice in the figure above that the ‘Zoom Out’ Button assigns a value of ‘zoomoutIcon’ to the ‘iconClass’ attribute. This simply points to a CSS style that has been defined at the top of the file. Notice in the figure below that we have a style called ‘zoomoutIcon’ which defines an image to use for our button.
With a Button dijit you can also include text along with the icon. For this button we include the text ‘Zoom Out’ inside the <div> tag that creates the button.
The resulting button with icon and text is seen below.
So, that about sums it up for the user interface portion of the toolbar. Next we need to add in the code that creates an instance of esri.toolbars.Navigation and hook it into our map and toolbar.
Creating an instance of the Navigation class is as easy as calling the constructor and passing in a reference to esri.map as seen in the code example below. However, you’ll first want to make sure that you add a reference to esri.toolbars.navigation.
Finally, we use dojo.connect to wire up an event to an event handler. In this case we are connecting the ‘onExtentHistoryChange’ event to the ‘extentHistoryChangeHandler’ function will run when the extent history changes.
How it Works
Now I’ll draw your attention back to the <div> tags where we created our toolbar and buttons. The Navigation class contains a number of constants and methods as seen below.
Each of the buttons that we defined within <div> tags has an ‘onClick’ attribute which refers to an event that fires when the button is clicked. In the case of the ‘Zoom In’ button seen in the code example below, a click of the button calls the ‘activate’ method on Navigation. A navigation type is passed into the ‘activate’ method. The navigation type can be one of three constants: PAN, ZOOM_IN, ZOOM_OUT. In each case these buttons are really tools that will require an additional map action by the user. For example, in the case of ‘ZOOM_IN’ the user would then draw a rectangle on the map which would trigger the generation of a new map displayed to the user.
In addition to zooming in and out and panning the Navigation class also supports full extent, next extent, and previous extent functions as defined through their corresponding methods (zoomToFullExtent(), zoomToNextExtent(), zoomToPrevExtent()). These methods are invoked through the ‘onClick’ event as seen below.