Search widget not suggesting/ not displaying any result from feature layer

693
2
07-11-2019 02:40 AM
reneeshtk
New Contributor

I have displayed a map using webmap and now trying to implement the search.. but it is not doing search in the feature layer.. What is wrong I am doing?

const testLayer = map.allLayers.find(function(layer) {
return layer.title === "mytestlayer";
});

var searchWidget = new Search({

view: view,
allPlaceholder: "Search",
sources: [
{
layer: testLayer,
searchFields: ["myfield"],
suggestionTemplate: "{myfield}",
exactMatch: false,
outFields: ["*"],
name: "testing",
placeholder: "example: test"
}
]
});

view.ui.add(searchWidget, {
position: "top-right"
});
});
0 Kudos
2 Replies
Egge-JanPollé1
MVP Regular Contributor

Hi reneesh tk‌,

Please find below the code for a working example of a Search Widget searching a Feature Layer.

It searches a Feature Layer with Dutch municipalities ("Gemeenten"): https://services.arcgis.com/nSZVuSZjHpEZZbRo/arcgis/rest/services/Bestuurlijke_Grenzen_Gemeenten_201... and if you type Amster it will come up with the suggestion Amsterdam :)

You can see the page in action here: Aan de slag met ArcGIS JavaScript - Feature zoeken 

I hope this example will help you to get your code up and running.

Best regards,

Egge-Jan

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>Aan de slag met ArcGIS JavaScript - Feature zoeken</title>
  <style>
    html, body, #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

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

  <script>  
      require([
        "esri/Map",
        "esri/geometry/Point",
        "esri/views/MapView",
		"esri/layers/FeatureLayer",
		"esri/widgets/Search"
      ], function(Map, Point, MapView, FeatureLayer, Search) {

      var map = new Map({
        basemap: {
          portalItem: {
            id: "7aea6fa913a94176a1074edb40690318" // Topo RD
          }
        }
      });
      
	  var rdOrigin = new Point({
	    x: 155000,
	    y: 463000,
	    spatialReference: 28992
	  });
      
	  var view = new MapView({
	    spatialReference: 28992, 
	    container: "viewDiv",
	    map: map,
	    center: rdOrigin,
	    zoom: 3
	  });

      var featureLayerGemeenten = new FeatureLayer({
        url: "https://services.arcgis.com/nSZVuSZjHpEZZbRo/arcgis/rest/services/Bestuurlijke_Grenzen_Gemeenten_2019/FeatureServer/0",
      popupTemplate: {
          // autocasts as new PopupTemplate()
          title: "Gemeente {Gemeentenaam} </br>CBS Code: {GM_Code}",
          overwriteActions: true
        }
      });

      var searchWidget = new Search({
        view: view,
        sources: [
          {
          layer: featureLayerGemeenten,
          searchFields: ["Gemeentenaam", "GM_Code"],
          suggestionTemplate: "{Gemeentenaam} ({GM_Code})",
          exactMatch: false,
          outFields: ["Gemeentenaam", "GM_Code"],
          placeholder: "Zoek gemeente (naam of code)"
          }
		],
        includeDefaultSources: false
      });
	  
	  view.ui.add([searchWidget], "top-right");
    });
  </script>
</head>
<body>
  <div id="viewDiv"></div>
</body>
</html>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Noah-Sager
Esri Regular Contributor

We also have a hosted sample that shows how to work with the Search widget and setting the source to a FeatureLayer:

https://developers.arcgis.com/javascript/latest/sample-code/widgets-search-multiplesource/index.html

0 Kudos