Search widget with multiple sources

Explore in the sandboxView live

This sample demonstrates how to use the Search widget to search multiple Layer Sources based on given fields. The search widget provides the capability to search features in Layers or geocode locations with a Locator. By default, the Search widget sets the view on the Search result. The level of detail (LOD) at the center of the view depends on the data source, with higher quality data sources returning extents closer to the feature obtained from the search.

To use multiple sources with the Search widget, you must set the widget's sources property.

The geocoding service requires a token for authentication. This sample uses an apiKey to authenticate. You can either replace it with your own apiKey, or remove it and log in once prompted. Alternatively, you can use another authentication method to access the geocoding service.

                                                                                                                 
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <title>Search widget with multiple sources | Sample | ArcGIS API for JavaScript 4.19</title>

    <style>
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>

    <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css" />
    <script src="https://js.arcgis.com/4.19/"></script>

    <script>
      require(["esri/Map", "esri/views/MapView", "esri/layers/FeatureLayer", "esri/widgets/Search", "esri/tasks/Locator"], function(
        Map,
        MapView,
        FeatureLayer,
        Search,
        Locator
      ) {
        const map = new Map({
          basemap: "dark-gray-vector"
        });

        const view = new MapView({
          container: "viewDiv",
          map: map,
          center: [-97, 38], // lon, lat
          scale: 10000000
        });

        const featureLayerDistricts = new FeatureLayer({
          url:
            "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/CongressionalDistricts/FeatureServer/0",
          popupTemplate: {
            // autocasts as new PopupTemplate()
            title: "Congressional District {DISTRICTID} </br>{NAME},{PARTY}",
            overwriteActions: true
          }
        });

        const featureLayerSenators = new FeatureLayer({
          url: "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/US_Senators/FeatureServer/0",
          popupTemplate: {
            // autocasts as new PopupTemplate()
            title: "<a href={Web_Page} target='_blank'> {Name}</a>, ({Party}-{State}) ",
            overwriteActions: true
          }
        });

        const searchWidget = new Search({
          view: view,
          allPlaceholder: "District or Senator",
          includeDefaultSources: false,
          sources: [
            {
              layer: featureLayerDistricts,
              searchFields: ["DISTRICTID"],
              displayField: "DISTRICTID",
              exactMatch: false,
              outFields: ["DISTRICTID", "NAME", "PARTY"],
              name: "Congressional Districts",
              placeholder: "example: 3708"
            },
            {
              layer: featureLayerSenators,
              searchFields: ["Name", "Party"],
              suggestionTemplate: "{Name}, Party: {Party}",
              exactMatch: false,
              outFields: ["*"],
              placeholder: "example: Casey",
              name: "Senators",
              zoomScale: 500000,
              resultSymbol: {
                type: "picture-marker", // autocasts as new PictureMarkerSymbol()
                url: "/javascript/latest/sample-code/widgets-search-multiplesource/live/images/senate.png",
                height: 36,
                width: 36
              }
            },
            {
              name: "ArcGIS World Geocoding Service",
              placeholder: "example: Nuuk, GRL",
              apiKey: "AAPKd6517aa887304b5891f6b959ea426015CLWBA2qIMPI4-vgwnS0B8RGRBVMArpJu0IN2BUL-G6GZ_aa8NF-r_JvSnsWp_A2M",
              singleLineFieldName: "SingleLine",
              locator: new Locator({
                url: "https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer"
              })
            }
          ]
        });

        // Add the search widget to the top left corner of the view
        view.ui.add(searchWidget, {
          position: "top-right"
        });
      });
    </script>
  </head>

  <body>
    <div id="viewDiv"></div>
  </body>
</html>