How to remove graphics from a label layer

487
2
Jump to solution
05-11-2022 07:44 AM
FranklinAlexander
Occasional Contributor III

I have created a label layer and it is working fine except that I cannot remove a specific graphic from the layer. I can clear the graphics using layer.clear(), but I cannot seem to clear a single graphic from the layer because there don't appear to be any graphics in the layer even thought they are displaying on the map. I cannot find a graphics property, and the _graphicsVal property is empty []. Does anyone know what I am missing? The layer represents labels for a polygon layer and if I clear a particular polygon, I need to be able to clear the label for that polygon. Here is my code for how I am creating the label layer and adding the graphics:

// adding the label layer
 var layerDefHab = {
          "geometryType": "esriGeometryPoint",
          "fields": [{"name": "Label", "type": "esriFieldTypeString","alias": "Label"},
                     {"name": "Species", "type": "esriFieldTypeString", "alias:": "Name"}]
        }
        var fcHab = {
          layerDefinition: layerDefHab,
          featureSet: null
        };

        this._labelLayer = new FeatureLayer(fcHab, {
          id: "Result Labels",
          showLabels: true,
          outfields: "*",
          minScale: 5000
        });
        this.map.addLayer(this._labelLayer, 6);
        this._addNewLabelClass(this._labelLayer);

// adding graphics to the layer
array.forEach(lang.hitch(this, function(arr) {
          let text = arr[1] + ": " + arr[2] + arr[3];
          let attrs = arr[0].attributes;
          attrs.Label = text;
          let labelGraphic = new Graphic(arr[4], null, attrs, null);
          //console.log("label graphic ", labelGraphic);
          this._labelLayer.add(labelGraphic);          
        }))

//label class code
_addNewLabelClass: function(layer) {
        let symbolFont = this._getSymbolFont();
        let json = {
          "labelExpressionInfo": {"value": "{Label}"},
          "labelPlacement":"center-center",
          "minScale": "5000",
          "sizeInfo": {
            "field": "Label",
            "minSize": 8,
            "maxSize": 12,
            "minDataValue": 1,
            "maxDataValue": 50
          }
        };
        let lc = new LabelClass(json);
        lc.symbol = new TextSymbol({
          font: symbolFont,
          color: new Color([0,0,0]),
          haloColor: new Color([255,255,255]),
          haloSize: 2
        });
        layer.setLabelingInfo([ lc ]);
      },

 

I know I am missing something obvious, so I appreciate any help.

0 Kudos
1 Solution

Accepted Solutions
FranklinAlexander
Occasional Contributor III

I am using 3.x and I need to start specifying that in my posts. Actually, I think I found the issue. I was actually clearing out the labelLayer graphics in another function and didn't realize, so just a stupid mistake. I now have access to the graphics! Thanks!

View solution in original post

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

In 4.x when you create a featurelayer (FL) from source graphics you need to query the FL to get its graphics. You are not able to just iterate of a graphics property like you use to in 3.x. To delete a feature you would need to use applyEdits on the featurelayer.

const queryParams = this._labelLayer.createQuery();
queryParams.where = 'your search criteria'
this._labelLayer.queryFeatures(queryParams).then(function(results){
  //now use the results that matched your criteria to delete that feature
  this._labelLayer.applyEdits({deleteFeatures:results.features});
});
0 Kudos
FranklinAlexander
Occasional Contributor III

I am using 3.x and I need to start specifying that in my posts. Actually, I think I found the issue. I was actually clearing out the labelLayer graphics in another function and didn't realize, so just a stupid mistake. I now have access to the graphics! Thanks!

0 Kudos