AnsweredAssumed Answered

JavaScript API 4.0, Get layer from graphic object using search widget

Question asked by on Jun 8, 2016
Latest reply on Dec 5, 2016 by dcoley

Javascript API 4.0

I am wondering that why i am not able to get layer from graphic object when i clicked on searched item using search widget.

Here are the scenario.


1. Create Map, View and add a feature layer

2. Add on click event in view and implement some features on click event

2. Create search widget assign view in constructor.

3.Push sources i.e. sources.push( assign feature later with popup template and other features)

4. Add search widget and startup.

5. Search Something, Search widget searched and zoom to that point.

Now when we click on same searched point then in response of view click event we are not getting the graphic.layer info, whereas when we click on other point of feature layer then we gets graphic.layer object. But not getting the same object when we clicked on searched point.


view.on("click", function (event) {


                // get the returned screenPoint and use it

                // with hitTest to find if any graphics were clicked

                // (using promise chaining for cleaner code and error handling)

                view.hitTest(event.screenPoint).then(function (response) {

                    // we're only interested in the first result

                    var result = response.results[0];

                    if (result && result.graphic) {

                        return result.graphic;


                }).then(function (graphic) {

                    var layer = graphic.layer;

in above code , var layer = graphic.layer; we are not getting graphic.layer; info when we click on search item searched by search widget, but when we click on other feature on feature layer then we gets graphic.layer info.


Is there any clue on this ?

I am doing normal things like


/*** Initalize Map ***/

            map = new Map();

/*** Initalize MapView ***/

            view = new MapView({

                container: "map",

                map: map,

                constraints: {

                    minZoom: 2,

                    maxZoom: 11



//Add Search Widget

          var searchWidget = new Search({ view: view });

           var sources = searchWidget.get("sources");


                                            featureLayer: new FeatureLayer({

                                                url: lyr.url

                                                , popupTemplate: lyr.popupTemplate

                                                , opacity: lyr.opacity

                                                , outFields: ["*"]


                                            searchFields: ["somefield"],

                                            displayField: "somefiled",

                                            exactMatch: true/false,

                                            outFields: ["*"],

                                            name: "somestring",

                                            placeholder: "somestring",

                                            maxResults: 8,

                                            maxSuggestions: 6,

                                            popupEnabled: true,

                                            popupOpenOnSelect: true,

                                            minSuggestCharacters: 0



something like that...