Hi - I am struggling with what I believed was asynchronous behavior from the setVisibility method on a feature layer. I have a huge feature layer that, to avoid issues, has visibility set to false on load. After clearing the layer's graphics and adding a new, smaller set of graphics, I was setting visibility to 'true'. However all of the thousands of feature graphics kept showing up anyways. I tried using Dojo deferreds and rearranging when I changed visibility to no avail. The only thing that worked was changing the visibility, letting the entire map update, then clearing the graphics.
console.log('set visibility');
fl.setVisibility(true);
var wait = on(map, 'update-end', function() {
// Function to clear graphics
fl.clear();
fl.add(graphicSubset);
wait.remove();
});
However, this still flashes all of the layer's graphics before clearing them. Suspending drawing on the feature layer did not stop the flashing problem, and if I change the graphics first then wait and change visibility, it overrides the changes I made and loads the original layer's thousands of graphics when it is made visible.
So I am wondering what setVisibility() is doing to override what I input as graphics. I can see the altered layer with 28 features in the console log if I do not change visibility and the visibility is set to false, but if I change the graphics to the 28 features (results from a query) then set visibility to true after all updates are done, 1500 graphics are returned from the original feature layer even though I cleared them all...
What mode are you using when adding your feature layer? - MODE_AUTO, MODE_ONDEMAND, MODE_SELECTION, MODE_SNAPSHOP
This setting can impact what features are loaded and when they are loaded.
Do you have the full page code available?
Did you try using setDefinitionExpression(expression) on the feature layer?
Thanks for the replies. I have both an ONDEMAND and SNAPSHOT feature layer I am doing this for. I will try to put together code to share but right now the application is so huge it would drive you bonkers to try to follow.
setDefinitionExpression is helpful, except I need to subset data by OBJECTID so that may get unwieldy. My sense is the syntax would be 'OBJECTID=1 and OBJECTID=2 and..."etc
Sarah,
If you are going to have a long list of ObjectIds the the better SQL will look like this:
OBJECTID IN (1,2,4,6,7)
Sarah, specific to the definition expression issue, you can use the IN operator in your where clause:
ObjectID IN (33,55,56,222)
It could go something kind of like this:
var objectIdField = featureLayer.objectIdField;
var objectIds = array.map(features,function(feature){
return feature.attributes[objectIdField];
});
var whereClause = lang.replace("{0} IN ({1})",[objectIdField,objectIds.join(",")]);
featureLayer.setDefinitionExpression(whereClause);
Thanks for that 'objectIdField' property tip John, I had not noticed that before.
Thanks everyone, I have finally managed a full workaround, though I still do not understand what was going on with the .setVisibility() method overriding my graphics. I chose to use setDefinitionExpression() because then 'ONDEMAND' feature layers did not fully redraw and have to be hidden after every map pan event, etc. I also used a QueryTask rather than the queryIds method of a feature layer to grab the objectids I needed since it queries the server and not client side, where the ids were being limited by the definition expression..