underbelly of .setVisibility() method for Feature Layer?

1685
7
08-11-2014 04:46 PM
by Anonymous User
Not applicable

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

0 Kudos
7 Replies
OwenEarley
Occasional Contributor III

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?

0 Kudos
RiyasDeen
Occasional Contributor III

Did you try using setDefinitionExpression(expression) on the feature layer?

featurelayer | API Reference | ArcGIS API for JavaScript

by Anonymous User
Not applicable

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

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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)

0 Kudos
JohnGrayson
Esri Regular Contributor

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

by Anonymous User
Not applicable

Thanks for that 'objectIdField' property tip John, I had not noticed that before.

0 Kudos
by Anonymous User
Not applicable

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

  1. I have the 'visibility : true' set for all layers involved
  2. I make them all MODE_ONDEMAND (SNAPSHOT mode was weirdly saving previous graphics from prior definition expressions sometimes)
  3. I set their definition expression on load to something that returns null so that no features are displayed. This gets around the setVisibility method problem, so I never have to change it.
  4. I run a query that gathers the objectids I need to display
  5. I set the definition expression using those objectids to display the specific data I need to show
0 Kudos