1

I am using ArcGIS API for JavaScript 3.7.

I am trying to see if I can select features in the same fashion that shift+click and dragging lets you zoom into the map by drawing a rectangle. The trouble that I am encountering is that I want this functionality on a certain layer of the map, but I do not know if this layer is ready yet. I've tried using the following:

map.on('load', function initSelectToolbar(event) {
    selectionToolbar = new Draw(event.map);
    var selectQuery = new Query();

    on(selectionToolbar, "DrawEnd", function (geometry) {
        selectionToolbar.deactivate();
        selectQuery.geometry = geometry;
        featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW);
    });
});

I would like to see if it is possible to run this code when I know for sure that the layer is ready. As far as I am aware, I can't change the feature layer the code references once it is loaded in.

EDIT: It looks like I got it to work. I found a way to grab the layers URL using map.getLayer(layerId).url (how I missed that is beyond me), created a new feature layer for selection, and removed it when the user was done. I still think this could be done better though.

1 Answer 1

2

You can check if a specific layer is loaded (using map.addLayer) with the map's layer-add-result event. Since this event fires when any layer is loaded (including the basemaps), you have to check for the specific layer

map.addLayer(featureLayer);
map.on('layer-add-result', function(addLayer){
  if (addLayer.layer === featureLayer){
    console.log(featureLayer);
  }
});

If you're using map.addLayers to add multiple layers, you can use the layers-add-result event

map.addLayers([featureLayer]);
map.on('layers-add-result', function (){
  console.log(featureLayer);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.