Select to view content in your preferred language

How to add features using SELECTION_ADD ?

1173
3
11-29-2011 02:19 PM
SowjanyaSunkara
Emerging Contributor
I'm working on the following scenario and need help with selection_ADD and selection_SUBTRACT.

1. User selects a bunch of features (a New selection) on the map using a selection tool:
fLayer = new esri.layers.FeatureLayer(selUrl, {
            mode: esri.layers.FeatureLayer.MODE_SELECTION,
            outFields: ["*"]
        });
        map.addLayer(fLayer);
var selSymbol = new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([255, 255, 0, 0.5]));
fLayer.setSelectionSymbol(selSymbol);
    var selResults = "";

    selectQuery = new esri.tasks.Query();
    selectQuery.geometry = geometry;

fLayer.selectFeatures(selectQuery, esri.layers.FeatureLayer.SELECTION_NEW, function (features) {


            alert("NEW " + features.length);

        });
2. Then the user selects a second bunch of features ( add to selection) and these features have to be added to the already existing selection set.
fLayer.selectFeatures(selectQuery, esri.layers.FeatureLayer.SELECTION_ADD, function (features) {

            for (var x = 0; x < features.length; x++) {
                selResults = selResults + ";" + features.attributes['X_REF'];
            }

            alert("APN:" + selResults);


        });

When I do this, the map shows all selected features (graphics) but I'm unable to get ALL the feature attributes. Only the last (recent) selected attributes are returned.

Any ideas how to make the Feature layer retuen all the Feaures?
0 Kudos
3 Replies
KellyHutchins
Esri Notable Contributor
Have you tried using the featureLayer.getSelectedFeatures method? This method returns an array of graphics for all the selected features in the feature layer.
0 Kudos
SowjanyaSunkara
Emerging Contributor
Yes. Thank you for your response.
I was able to get it working.
0 Kudos
MeleKoneya
Frequent Contributor
Kelly and Sowjanya,

I am having trouble with getting the getSelectedFeatures method to show the current set of selected Features.   Can you provide an example of what worked for you?

I was able to get some success by using the "onselectioncomplete" event, but it fires often and the count did not seem correct.

Thanks

Mele

function initSelectToolbar() {
            
            var selectQuery = new esri.tasks.Query();
            dojo.connect(map, "onClick", function (evt) {
             var layerInfos = hydrMSL.layerInfos;
             var visible = hydrMSL.visibleLayers;
              for (var i = 0; i < layerInfos.length; i++)
               {
                    layer = layerInfos;
                    if (visible.indexOf(layer.id) > -1)    
                    {
                            var FL = find_in_array(FLayers, layer.id);
                            FL.setSelectionSymbol(selectionSymbol);
                           
                                selectMode;
                                    if(evt.ctrlKey)  {
                                        selectMode = esri.layers.FeatureLayer.SELECTION_SUBTRACT;
                                    }
                                    else
                                    {
                                        selectMode = esri.layers.FeatureLayer.SELECTION_ADD; 
                                    };

                                    var centerPoint = new esri.geometry.Point
                                    (evt.mapPoint.x, evt.mapPoint.y, evt.mapPoint.spatialReference);
                                    var mapWidth = map.extent.getWidth();

                                    //Divide width in map units by width in pixels
                                    var pixelWidth = mapWidth / map.width;

                                    //Calculate a 10 pixel envelope width (5 pixel tolerance on each side)
                                    var tolerance = 10 * pixelWidth;

                                    //Build tolerance envelope and set it as the query geometry
                                    var queryExtent = new esri.geometry.Extent
                                    (1, 1, tolerance, tolerance, evt.mapPoint.spatialReference);
                                    selectQuery.geometry = queryExtent.centerAt(centerPoint);

                                    //selectQuery.geometry = evt.mapPoint;
                                    
                                    FL.selectFeatures(selectQuery, selectMode, function (features) {
                                        if (features.length > 0) {
                               
                                        } else {
                                           
                                        }
                                    });
                                                                                  

                       //   dojo.connect(FL, "onSelectionComplete", selectMode, function(){
                        //  for (var x = 0; x < FLayers.length; x++)  {
                         // var selected = FLayers.getSelectedFeatures();
                         // var selected = FL.getSelectedFeatures();
                         // alert(selected.length);
                         // for (i =0; i < selected.length; i++)
                         // {
                         //   var row = selected;
                         // }

                        //};    
                                    
                   // });
         
                    
                    };
               }     
          
            });

             function find_in_array(arr, value) {
                var FLayer;
                for (var i = 0, len = FLayers.length; i<len; i++) {
                    if (arr.layerId == value) return FLayers;
                };

            }
            
        };
0 Kudos