Selecting multiple features with graphics

624
8
Jump to solution
10-11-2018 08:27 AM
andrewdartez
New Contributor II

I am trying to select multiple polygon features and have a graphic drawn on top of the features to show selection with this code. Right now, the selection works and selects the features (I know this because it shows as selected in the featuretable). But the graphic only draws over a single feature. Is there a way to have it draw over multiple features?

var selectionToolbar;

map.on("load", initSelectToolbar);

var selectline = new SimpleLineSymbol();
selectline.setWidth(4);
selectline.setColor(new Color([0,255,255, 1]));
var selectlineSymbol = new SimpleFillSymbol();
selectlineSymbol.setOutline(selectline);
selectlineSymbol.setColor(new Color([46,191,240, 0]));

on(dom.byId("selectFieldsButton"), "click", function () {
selectionToolbar.activate(Draw.EXTENT);
});
on(dom.byId("clearSelectionButton"), "click", function () {
featurelayername.clearSelection();
map.graphics.clear();
});
function initSelectToolbar (event) {
selectionToolbar = new Draw(event.map);
var selectQuery = new Query();
on(selectionToolbar, "DrawEnd", function (geometry) {
selectionToolbar.deactivate();
selectQuery.geometry = geometry;

selectQuery.returnGeometry = true;
selectQuery.where = "1=1";

featurelayername.queryFeatures(selectQuery, function (featureSet) {
featurelayername.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW);
var Geom = featureSet.features[0].geometry;
var gra = new Graphic(Geom, selectlineSymbol);
map.graphics.add(gra);
});

});
}
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Honored Contributor

You're only creating a new graphic from the first feature in the featureset with this line.

var Geom = featureSet.features[0].geometry;

You'll want to loop through the featureset and do that to each feature.

array.forEach(featureSet, function (feature) {
  Geom = feature.geometry; 
  gra = new Graphic(Geom, selectlineSymbol); 
  map.graphics.add(gra); 
});

View solution in original post

8 Replies
KenBuja
MVP Honored Contributor

You're only creating a new graphic from the first feature in the featureset with this line.

var Geom = featureSet.features[0].geometry;

You'll want to loop through the featureset and do that to each feature.

array.forEach(featureSet, function (feature) {
  Geom = feature.geometry; 
  gra = new Graphic(Geom, selectlineSymbol); 
  map.graphics.add(gra); 
});
andrewdartez
New Contributor II

I incorporated this way. Cant get the graphics to show up.

featurelayername.queryFeatures(selectQuery, function (featureSet) {

array.forEach(featureSet, function (feature) {
   var Geom = feature.geometry;
   var gra = new Graphic(Geom, selectlineSymbol);
   map.graphics.add(gra);
});

});

0 Kudos
KenBuja
MVP Honored Contributor

Are you seeing any messages in the console?

0 Kudos
andrewdartez
New Contributor II

init.js:89 g {message: "Timeout exceeded", response: {…}, status: undefined, responseText: undefined, xhr: XMLHttpRequest, …}

also:

Active resource loading counts reached to a per-frame limit while the tab is in background. Network requests will be delayed until a previous loading finishes, or the tab is foregrounded. See <URL> for more details

0 Kudos
KenBuja
MVP Honored Contributor

Did you add the module for array?

require([...,"dojo/_base/array",...], function(...,array,...){
0 Kudos
andrewdartez
New Contributor II
var selectionToolbar;

map.on("load", initSelectToolbar);

var selectline = new SimpleLineSymbol();
selectline.setWidth(4);
selectline.setColor(new Color([0,255,255, 1]));
var selectlineSymbol = new SimpleFillSymbol();
selectlineSymbol.setOutline(selectline);
selectlineSymbol.setColor(new Color([46,191,240, 0]));

on(dom.byId("selectFieldsButton"), "click", function () {
selectionToolbar.activate(Draw.EXTENT);
});
on(dom.byId("clearSelectionButton"), "click", function () {
JobStatusLyr.clearSelection();
map.graphics.clear();
});
function initSelectToolbar (event) {
selectionToolbar = new Draw(event.map);
var selectQuery = new Query();
on(selectionToolbar, "DrawEnd", function (geometry) {
selectionToolbar.deactivate();
selectQuery.geometry = geometry;

selectQuery.returnGeometry = true;
selectQuery.where = "1=1";

JobStatusLyr.queryFeatures(selectQuery, function (featureSet) {

array.forEach(featureSet, function (feature) {
var Geom = feature.geometry;
var gra = new Graphic(Geom, selectlineSymbol);
map.graphics.add(gra);
});
});
JobStatusLyr.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW);
});
}


});
0 Kudos
JohnGrayson
Esri Regular Contributor

Q: Is featureSet a FeatureSet or an array of Graphic objects?  If its a FeatureSet, then try looping over the 'features' property which is an array of Graphic objects: 

array.forEach(featureSet.features, function (feature) {
  // ... //
});
andrewdartez
New Contributor II

That did it. Great thanks.

0 Kudos