Lang.Hitch selectFeatures in featureCollection

867
7
10-24-2017 12:30 PM
ThaoLe1
New Contributor III

I have these codes.  I'm wondering how do I use Lang.Hitch for this because of this error TypeError: Cannot read property 'selectFeatures' of undefined.

function CreateGraphics (evt) {

CODES HERE.......................

var graphics5= new Graphic(geoBuffer, symbolBuffer, attr5, infoTemplate5);
//map.graphics.add(graphics5);
ShowSelectedFeatures (graphics5);
}

/////////////////////////////////////////////////////////////////////////////////////

function ShowSelectedFeatures(graphicsLine) {

var featureCollection = {
"layerDefinition": null,
"featureSet": {
"features":graphicsLine,
"geometryType": "esriGeometryLine"
}
};

featureCollection.layerDefinition = {
"geometryType": "esriGeometryLine",
"objectIdField": "ObjectID",

"fields": [{
"name": "ObjectID",
"alias": "ObjectID",
"type": "esriFieldTypeOID"
}]
};


// Create a new feature layer
var FLayer = new FeatureLayer(featureCollection, {
mode: FeatureLayer.MODE_SELECTION,
outFields: ["*"],
infoTemplate: infoTemp
});

FLayer.setSelectionSymbol(lineSymb);
map.addLayer(FLayer);

}


//////////////////////////////////////////////////////////////////////////////////////

FLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function (evt) {

CODES HERE .................................

});

0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus

Thao,

   It looks like your FLayer is a local var inside of the ShowSelectedFeatures function and that your call to FLayer.selectFeatures is outside of that functions scope. So in that case the FLayer is always going to be null unless you declare the FLayer var higher in your apps scope. So you need to declare the FLayer var at the begining of your app and then inside the ShowSelectedFeatures you would just use 

// Create a new feature layer
FLayer = new FeatureLayer(featureCollection, {
0 Kudos
ThaoLe1
New Contributor III

Hi Robert,

I declared a global variable at the beginning of the app

var FLayer;

and inside ShowSelectedFeatures() I used your suggestion. Still same error.

// Create a new feature layer
FLayer = new FeatureLayer(featureCollection, {
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Thao,

   I would have to see more of your code then.

0 Kudos
ThaoLe1
New Contributor III

Robert, could you verify the ShowSelectedFeatures() function for featureCollection and its syntax is reasonably correct? 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Thao,

   I am not sure a FeatureLayer created from a FeatureCollection supports the selectFeature method. I will test some and get back with you.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Thao,

   So geometry based queries work fine but a query that uses a where clause is not supported.

0 Kudos
thejuskambi
Occasional Contributor III

The error message is indicating that the Flayer variable is not initialized at the time the selectFeatures is called. You need ensure that the Flayer is initialized and or loaded on the the map before selecting features from it.

I would suggest to move the selectFeatures code in ShowSelectedFeatures method and also call it after the layer is loaded/added to map event.