Hi Guys,
On my map i want to select multiple layers by drawing rectangle and than export selected features fields. i have this code thanks to @BlakeTerhune (From my previous topic😞
// create a new sketch view model set its layer
const sketchViewModell = new SketchViewModel({
view: view,
layer: polygonGraphicsLayerr
});
// Once user is done drawing a rectangle on the map
// use the rectangle to select features on the map and table
sketchViewModell.on("create", async (event) => {
if (event.state === "complete") {
// this polygon will be used to query features that intersect it
const geometriess = polygonGraphicsLayerr.graphics.map(function(graphic){
return graphic.geometry
});
const queryGeometry = await geometryEngineAsync.union(geometriess.toArray());
selectFeatures(queryGeometry);
}
});
// This function is called when user completes drawing a rectangle
// on the map. Use the rectangle to select features in the layer and table
function selectFeatures(geometry) {
// create a query and set its geometry parameter to the
// rectangle that was drawn on the view
var query = {
geometry: geometry,
outFields: ["*"]
};
map.allLayers.forEach(layer => {
// Query all layers data
layer.queryFeatures(query)
.then((results) => {
// do something with query results
console.log(results)
})
.catch(errorCallback);
});
};
When i draw rectangle in logs i get error: layer.queryFeatures is not a function;
what i do wrong? What is wrong here and i can not see? Help
Solved! Go to Solution.
Your code is assuming that all layers in your map are FeatureLayers, but that is unlikely. You should therefore check the layer type before trying to execute a query on it. One simple check would be like so:
map.allLayers.forEach(layer => {
// Query all layers data
if (typeof layer.queryFeatures == "function") {
layer.queryFeatures(query).then((results) => {
// do something with query results
console.log(results)
}).catch(errorCallback);
}
});
Your code is assuming that all layers in your map are FeatureLayers, but that is unlikely. You should therefore check the layer type before trying to execute a query on it. One simple check would be like so:
map.allLayers.forEach(layer => {
// Query all layers data
if (typeof layer.queryFeatures == "function") {
layer.queryFeatures(query).then((results) => {
// do something with query results
console.log(results)
}).catch(errorCallback);
}
});
JoelBennett is correct.
map.allLayers.forEach(layer => {
// Query all layers data
if (layer.type === "feature" && layer.visible){
layer.queryFeatures(query)
.then((results) => {
// do something with query results
console.log(results)
})
.catch(errorCallback);
}
});
Thank You guys @JoelBennett @LefterisKoumis , both code works fine, simply checking layer type 😉 Thanks again for help.