When I click on a feature in my feature layer, what is being returned to me?

1067
5
Jump to solution
05-02-2018 09:40 AM
MKF62
by
Occasional Contributor III

I realize this is a pretty vague question, but I'm having trouble wrapping my brain around it. Say I have a feature layer loaded up, it's made up of polygons. If I click on the one of the polygons, the blue selection outline shows up to indicate which one I have clicked on, and I assume some sort of object is being returned to me. What is it and how do I access it? By default each map has a GraphicsLayer so I *think* the object being returned is a graphic. I'd like to learn how to access the geometry of it so I can start doing simple calculations like acreage on whatever polygon the user has clicked. 

It appears map.graphics will return an array of the graphics that make up the GL in your map (of which I only have the default, I have not added any of my own). If I were to click on a polygon, would you have to use the method .getSelectedFeatures() or something to get the single graphic of interest? Or would you do something like map.graphics[0]? When I have tried

console.log(map.graphics[0]);

I get "undefined" in the console. Though if I do console.log(map.graphics) I can see there is 1 graphic in the array with an index of 0. I'm just not sure where to go from here.

If I can get the graphic then I can use the geometry property to start working with it.

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Molly,

   When you click on a feature that has an InfoTemplate or PopupTemplate defined then the selection symbol from the Popup (the map.infoWindow) it added to a special graphic from the popup. If you want access to the graphic you have to get the selection from the popup:

var graphic = map.infoWindow.getselectedfeature();

View solution in original post

0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus

Molly,

   When you click on a feature that has an InfoTemplate or PopupTemplate defined then the selection symbol from the Popup (the map.infoWindow) it added to a special graphic from the popup. If you want access to the graphic you have to get the selection from the popup:

var graphic = map.infoWindow.getselectedfeature();
0 Kudos
MKF62
by
Occasional Contributor III

What you're saying makes perfect sense but I couldn't get it to work that way, despite having an infoTemplate set for the feature layer being clicked on. I did find it works like this though:

patchesFL.on("click", function (evt) {
    var graphic = evt.graphic.geometry;
});‍‍‍‍‍‍

This didn't work:

var graphic = map.infoWindow.getSelectedFeature();

Nor did this (I used popup instead of infoWindow because the documentation lists the method getSelectedFeature as a popup method not an infoWindow method):

var graphic = map.popup.getSelectedFeature();‍‍
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Molly,

   Because you are using a FeatureLayer you can use the on click method to get the graphics like you have there. A maps infoWindows default to a Popup dijit in the API so you just need map.infoWindow (not map.popup). So the Question is where were you putting that line of code? Was there actually a selected feature yet? You should try to get the popups selected feature after you know the popup is shown. For that you can use the popups show event, selection-change event or set-features event.

0 Kudos
MKF62
by
Occasional Contributor III

Ah, I see now. Thanks! I got it working when I moved it to an appropriate spot. Before, I had it inside this:

patchesFL.on("click", function (evt) {
    var graphic = map.infoWindow.getSelectedFeature();
});

While the popup does open and a feature gets selected in the onClick function above, I think it just doesn't register fast enough. I needed to get the geometry inside of a different function called by a link in the action pane anyway, so when I moved the line of code there, it worked.

0 Kudos
KenBuja
MVP Esteemed Contributor

When you use the FeatureLayer click event, the graphic is returned to you.

The returned object contains screenPoint, mapPoint, and Graphic.