Mastering ArcGIS Server Development with JavaScript
上QQ阅读APP看书,第一时间看更新

Toolbars

The ArcGIS API for JavaScript uses toolbars to handle controls that transform the cursor. The API has three different toolbars: one for map navigation, one for drawing graphics, and one for editing those graphics. These tools are turned on using the activate() method, which includes parameters describing what tool you want to use. The tools can be turned off using the deactivate() method; let's take a closer look at these tools.

The navigation toolbar

The navigation toolbar handles advanced map navigation. By default, the map offers zoom sliders, and the ability to pan around the map. By activating the zoom in/zoom out features of the navigation toolbar, the user can draw a boxed extent where the map will either zoom in on, or zoom out at the same ratio as the zoom box to the map's current extent. This zoom in/out box is always on once you activate it, so you'll have to either develop something that turns it off, or let the user activate the pan tool, which disables the zoom in/out.

The navigation controls have three more actions that can be activated. The zoomToFullExtent() function, when called from the navigation toolbar, triggers the map to zoom to its original map extent. The toolbar also keeps track of how many zoom-ins, zoom-outs, and pans the user has done. By activating the zoomToPreviousExtent() function, the map extent goes back to previous extents. And if the map goes back to previous extents, the map can also zoom to future locations. With the zoomToNextExtent() function, the user can undo the zoom extent changes they made when they viewed the previous extent.

The draw toolbar

The draw toolbar allows the user to draw on the map to provide input. Depending on which draw tool was activated, the user can draw a shape on the map, and any event listener attached to the draw-end event is called, running a function that receives the shape the user drew. Multiple draw-end events can be attached to the event listener if needed. Common draw-end events might include adding a drawing to the map, measuring the shape drawn by the toolbar, or using the shape drawn in a query. The tools also modify the tooltip of the map, giving directions about how to draw with them.

The ArcGIS API provides numerous shapes to use when drawing on the map. The toolbar can be activated using simple drawing tool constants like point, polyline, and polygon, which are activated by clicking each point on the map and double-clicking to complete. There are freehand versions of the polyline and polygon, which allow you to click and drag the mouse to draw a continuous line or shape, and release the mouse to stop drawing. Also included in the API are drawing tools for common shapes such as triangles, rectangles, circles, ovals, and arrows, just to name a few.

Tip

The different toolbars are not aware of each other. If you are not careful, you could activate both drawing and navigation tools at the same time, and their actions will occur simultaneously. You could draw a shape for a tool, and be zoomed into that area because the navigation tool wasn't told to deactivate. Plan ahead how you will disable one event when another is enabled.

The edit toolbar

What happens when you draw something on the map and you have made a mistake? How do you correct a graphic on the map? The edit toolbar allows you to change map graphics' shape, size, and orientation. It also works on text symbols, letting you change the text of a text graphic on the map.

When the edit toolbar is activated on a feature, it allows you to move, rotate, scale, or change the vertices of the feature. Moving lets you grab the feature and position it where you want. Rotating provides a point to click and drag around to re-orient the graphic. Scaling lets the user make the graphic larger or smaller. For editing vertices, the user can drag points around on the map, right-click to "delete" the point, and click between the graphic versions on "ghost points" to add new vertices to the map.

Please note that while the Edit Toolbar allows users to change graphics on the map, it doesn't have any features built in that let the user save their changes. When using the edit toolbar on graphics in an editable feature layer, you still will need a way to tell the feature layer to save changes. Graphics can be edited in the current session without saving the changes.