###Feature Layer Search
In this lab you will add a search widget search against a feature layer. The widget performs context-sensitve search as you type and then it will zoom to and highlight the feature selected. You can also format the data in the popup that appears.
In this lab it will search against the neighborhood polygon layer.
-
Click create_starter_map/index.html and copy the contents to a new jsbin.com.
-
In
JSBin
>HTML
, update therequire
statement and function definition:require(["esri/map", // Add Search, FeatureLayer and InfoTemplate modules "esri/dijit/Search", "esri/layers/FeatureLayer", "esri/InfoTemplate", "dojo/domReady!"], // Add Search alias function(Map,Search,FeatureLayer,InfoTemplate) { ...
-
Add the search widget's div:
<body> <div id="mapDiv"></div> <!-- ADD new div for the search widget --> <div id="searchDiv"></div> </body>
-
At the top of the page, add the CSS to position
searchDiv
:<style> html,body,#mapDiv { padding:0; margin:0; height:100%; } /* ADD search widget styling */ #searchDiv { display: block; position: absolute; z-index: 2; top: 20px; left: 74px; } </style>
-
Create the Search widget and start it up.
function(Map,Search) { map = new Map("mapDiv", { center: [-122.68, 45.52], zoom: 10, basemap: "dark-gray" }); // ADD the search widget var search = new Search({ map: map }, "searchDiv"); search.startup();
At this point, the map will allow you to search against the default ArcGIS Online Geocoding Service. Give it a go. You can enter an address or point of interest (like
Providence Park
orPDX
) or a geography (likeOregon
orUSA
). -
Configure the Search Widget to also search against the Neighborhoods Feature Service. Insert the following code directly before the
search.startup()
line added above.var sources = search.get("sources"); sources.push({ featureLayer: new FeatureLayer("http://services.arcgis.com/uCXeTVveQzP4IIcx/arcgis/rest/services/PDX_Neighborhoods_Styled/FeatureServer/0"), name: "Neighborhood Search", searchFields: ["NAME"], displayField: "NAME", exactMatch: false, outFields: ["NAME","AVGHINC_CY","MEDAGE_CY"], placeholder: "St. Johns", enableSuggestions: true, // Create an InfoTemplate for the popup infoTemplate: new InfoTemplate("Neighborhood","Name: ${NAME}</br>Avg. Household Income $ ${AVGHINC_CY}</br>Median Age: ${MEDAGE_CY}") }); search.set("sources",sources);
-
In JSBin, run the app > select the pulldown on the left-hand side of the search box > select
Neighborhood Search
from the pull down list > enter "St. John's". The app should highlight and zoom into the neighborhood polygon, and a popup should also be displayed.
Your app should look something like this:
###Bonus
- Add a Rail Stops feature layer to the Search widget.
- Customize the popup's CSS. Hint: see the Search with customization sample.