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.
Solved! Go to Solution.
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();
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();
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();
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.
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.
When you use the FeatureLayer click event, the graphic is returned to you.
The returned object contains screenPoint, mapPoint, and Graphic.