[GIS] Simulate a click event on FeatureLayer in ESRI JavaScript API

arcgis-javascript-api

When my app changes context (user clicks on a tab) I want to programmatically "Select" the first of 6 points in my Feature Layer so I get the blue box around the point and activate the info window.

I looked around the API Docs and found the FeatureLayer's selectFeatures method. This method takes a Query object, and a selection method (I'm creating a new selection so I'll use SELECTION_NEW). The function returns a promise, but do I really need any kind of callback? I'm just trying to select a feature.

Here is the definition of my feature layer:

var projectsAGOL = new esri.layers.FeatureLayer("http://services.arcgis.com/QVENGdaPbd4LUkLV/arcgis/rest/services/rioLocoSites/FeatureServer/0",{ 
    infoTemplate: infoTemplate,
    visible: true,
    outFields: ["*"]
});

Click handler for the Tab:

jQuery("#prjMap").click(function(){
    var query = new esri.tasks.Query(); // Create a Query Object
    query.where = "OBJECTID = 0"; // Select the first point from the Feature Layer
    projectsAGOL.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW); // Apply selection
});

This code does not produce the desired outcome nor does it produce an error. Nothing happens. Am I even using the correct method to make a selection? I'm more familiar with the Leafletjs mapping library.

Update: Provided jsFiddle

Best Answer

Try this:

jQuery("#prjMap").click(function(){
    var projectsAGOL = new esri.layers.FeatureLayer("http://services.arcgis.com/.../FeatureServer/0",{ 
        infoTemplate: infoTemplate,
        visible: true,
        outFields: ["*"]
    });

    var query = new esri.tasks.Query(); // Create a Query Object
    query.where = "FID= 0"; // Select the first point from the Feature Layer
    query.returnGeometry = true;
    query.outFields = ["*"]; // Return all attributes in the FeatureSet

    var selectionSymbol = new SimpleFillSymbol().setColor(new Color([255,255,0,0.5]));
    featureLayer.setSelectionSymbol(selectionSymbol); 
    projectsAGOL.selectFeatures(query,esri.layers.FeatureLayer.SELECTION_NEW);
}