concept Geocoder widget in category arcgis

appears as: Geocoder widget, The Geocoder widget, Geocoder widget
ArcGIS Web Development

This is an excerpt from Manning's book ArcGIS Web Development.

Now suppose you also want to load another widget from the ArcGIS API for JavaScript, such as the Geocoder widget, which allows you to search for addresses. That widget has options you can also define in config.json.

7.1.2. Loading the Geocoder widget

The Geocoder widget has a couple of required options, such as the URL for the Geocoder service and a placeholder value to put in the input box. Let’s update config.json with the following options for use in the application:

Before you load the Geocoder widget, let’s add a div element to the index.html file that will contain the search box for the widget. You can add it after the map-div:

<body class="nihilo">
    <div id="map-div"></div>
    <div id="search"></div>
    ...
  </body>

Next, add styling in main.css to position the Geocoder widget:

#map-div{
  ...
 }

 #search {
  position: absolute;
  z-index: 2;
  top: 5px;
  right: 5px;
 }

This code positions the Geocoder widget in the upper-right corner of the browser.

The last thing to do before you load the Geocoder widget is to update the main.js file, as shown in the following listing. This file waits for the map to finish loading, and then passes the map and the options from the config.json file to the Geocoder.

Listing 7.5. js/main.js—loading the Geocoder widget

Now the main.js file loads the Geocoder widget along with another utility module: the dojo/_base/lang module. When the map finishes loading, pass the map as part of the parameters for the Geocoder widget. The lang.mixin method mixes in the fields of one object with another. You can then pass the completed options to the Geocoder and run the startup method to display it on the page. If you run the application now, you’ll see a page similar to figure 7.2, as shown on the previous page.

Figure 7.2. Geocoder widget added to the application

You can search for an address in the Geocoder widget as you would with almost any search engine, and it attempts to zoom the map to the correct location.

I used the Geocoder widget to not only show you an example of how widgets are typically loaded with the ArcGIS API for JavaScript, but also to see whether you noticed a pattern with other widgets you’ve seen throughout the book. If not, don’t worry; I’ll cover that next.