I can see how to set and get a Feature Layer Definition Expression, but how can I remove a Definition Expression? I want to be able to switch between several different definition expressions.

926
3
Jump to solution
12-11-2017 11:51 AM
CharlesGeiger
New Contributor III

A semi-working copy of the page is at http://mapmaker2.millersville.edu/pamaps/PA_CountyHistories/.  The goal is to have the user click on the tabs to the left of the map.  The right two tabs include nested unordered lists, and each list item is a <button>.  In the function called by the button click, the current feature layer is removed and the new feature layer added.  At more detailed levels of the nested lists the added feature layer should have a subset of the features visible, by virtue of a setDefinitionExpression method.  I believe that the definition expression is not working because the previous definition expression is not being cleared before the new one is added.  The API Reference is very vague about that behavior, and the only methods there are "get..." and set..." .  There is no "clear" or "remove."  Any ideas?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Charles,

  OK I see your issue after looking at your site. You need to call refresh on the layer after you have added it to the map and set the definition expression (i.e. line 18).

        case 1: // choropleth of Types of county Names
          map.removeLayer(fL[curLyr]);
          fL[1].setDefinitionExpression("");
          if (cat1 != 0) {
            defExp = "nameType = '" + cat1 + "'";
            fL[1].setDefinitionExpression(defExp);
            if (cat2 != 0) {
              defExp = "nameType2 = '" + cat2 + "'";
              fL[1].setDefinitionExpression(defExp);
              if (cat3 != 0) {
                defExp = "nameType3 = '" + cat3 + "'";
                fL[1].setDefinitionExpression(defExp);
              }
            }
          }
          map.addLayer(fL[1]);
          curLyr = 1;
          fL[1].refresh();
          break;

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Charles,

   Have you tried:

featureLayer.setDefinitionExpression("");

or

featureLayer.setDefinitionExpression(null);
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Charles,

  OK I see your issue after looking at your site. You need to call refresh on the layer after you have added it to the map and set the definition expression (i.e. line 18).

        case 1: // choropleth of Types of county Names
          map.removeLayer(fL[curLyr]);
          fL[1].setDefinitionExpression("");
          if (cat1 != 0) {
            defExp = "nameType = '" + cat1 + "'";
            fL[1].setDefinitionExpression(defExp);
            if (cat2 != 0) {
              defExp = "nameType2 = '" + cat2 + "'";
              fL[1].setDefinitionExpression(defExp);
              if (cat3 != 0) {
                defExp = "nameType3 = '" + cat3 + "'";
                fL[1].setDefinitionExpression(defExp);
              }
            }
          }
          map.addLayer(fL[1]);
          curLyr = 1;
          fL[1].refresh();
          break;
CharlesGeiger
New Contributor III

Thank you, Robert.  It does turn out that the method featureLayer.setDefinitionExpression(...) will in itself remove any previous definition expression, so that wasn't my issue after all.  It was, as Robert showed, the need to refresh the feature layer.  I can, in fact add the feature layer as soon as I removed the previous one, the refresh just has to happen after I change the definition expression.  Thanks again.

0 Kudos