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

2544
4
06-08-2016 10:39 PM
SachinTiwari
New Contributor II

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) {

                //debugger;

                // 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");

sources.push({

                                            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...

0 Kudos
4 Replies
SachinTiwari
New Contributor II

this is the result when we search anything using search widget(using the same feature layer) and again click on same point.

result from search widget.png

this is the result when we click on feature layer.

result from feature click.png

0 Kudos
SachinTiwari
New Contributor II

feeling that the search widget is pointing to some other points and the feature layer is pointing to some other point for the same. How we can sync both so that they will point to the same.

0 Kudos
SachinTiwari
New Contributor II

but i m using the same feature layer in both.

1. Add the url in feature layer and add the layer in the map.

2. add the same feature layer in search widget sources.

0 Kudos
DavidColey
Frequent Contributor

Hi Sachin - not sure if you figured this out yet but I notice that you are using .get for .push to your sources array.  In my 4.x search I'm just adding souces as part of the Search's constructor options, In that way I have no need to set a sources var and get sources, then push.   Not sure if that helps you or not