I am using JS 4.30. I would like ot figure out how to get the legend to update the symbology of what is in the map when zoomed in. The legend for one of my layers has over 200 symbols. I have a fucntion to zoom into a specific building and floor. That building and floor does not have all 200 symbols it only has 10. I would like the legedn to update and show only the 10 symbols in the map.
The print funtion does this see attached.
I appreciate any help Thank you!
Solved! Go to Solution.
I took a closer look at your post from Monday, and think I see the problem. Here is what you posted reproduced for convenience:
function zoomRoomUse (ids)
{
require ([
"esri/rest/query",
"esri/rest/support/Query",
"esri/layers/FeatureLayer",
"esri/views/layers/LayerView"
],
function (query, Query, FeatureLayer, LayerView)
{
roomsUseLayer.definitionExpression = roomsUseLayer.objectIdField + " IN(" + ids.join (",") + ")";
// console.log (roomsUseLayer.definitionExpression)
view.whenLayerView (roomsUseLayer).then (function (layerView)
{
var query = roomsUseLayer.createQuery ();
query.objectIds = [ids];
query.where = roomsUseLayer.definitionExpression;
roomsUseLayer.queryFeatures (query).then (function (results)
{
const features = results.features;
var geometry = features[0].geometry.extent.clone ();
for (var k = 0; k < features.length; k++) {
geometry = geometry.union (features[k].geometry.extent.clone ());
view.goTo (geometry).then (function ()
{
roomsUseLayer.visible = true;
manageDynamicSymbology (roomsUseLayer, view);
}).catch (function (error)
{
if (error.name != "view:goto-interrupted") {
console.error (error);
} else {
console.log ("Drawing");
}
});
}
});
});
});
}
I think the main problem is that you're calling "manageDynamicSymbology" inside of a loop on line 27. The function manageDynamicSymbology should only ever be called one time, preferably when the layer is created. It never needs to be called again afterwards. For example:
var layer = new FeatureLayer({
url: "https://my/url/MapServer/0",
id: "myLayerID"
});
manageDynamicSymbology(layer, view);
view.map.add(layer);
Another problem is that you're also calling view.goTo inside the loop, but you want the loop to finish before calling that function. This way, you would only zoom one time to the full extent of the graphics. Here is the updated code:
function zoomRoomUse(ids)
{
require ([
"esri/rest/query",
"esri/rest/support/Query",
"esri/layers/FeatureLayer",
"esri/views/layers/LayerView"
],
function (query, Query, FeatureLayer, LayerView)
{
roomsUseLayer.definitionExpression = roomsUseLayer.objectIdField + " IN(" + ids.join (",") + ")";
// console.log (roomsUseLayer.definitionExpression)
view.whenLayerView (roomsUseLayer).then (function (layerView)
{
var query = roomsUseLayer.createQuery ();
query.objectIds = [ids];
query.where = roomsUseLayer.definitionExpression;
roomsUseLayer.queryFeatures (query).then (function (results)
{
const features = results.features;
var geometry = features[0].geometry.extent.clone();
for (var k = 1; k < features.length; k++) {
geometry = geometry.union (features[k].geometry.extent.clone());
}
view.goTo (geometry).then (function ()
{
roomsUseLayer.visible = true;
}).catch (function (error)
{
if (error.name != "view:goto-interrupted") {
console.error (error);
} else {
console.log ("Drawing");
}
});
});
});
});
}
Joel,
This is getting better! The only thing that isnt working is the intial zoom to still displays the whole legend not the dynamic part. If i pan or do an identify then the legend refreshes to the dynamic legend.
If i use roomsUseLayer.refresh() in the console that updates the legend.
Ok, I see the problem. In the "manageDynamicSymbology" function, you'll want to change this:
if (featureSet.features.length === 0)
featureLayer.renderer = originalRenderer;
else {
to this:
if (featureSet.features.length === 0) {
if (featureLayer.renderer != originalRenderer)
featureLayer.renderer = originalRenderer;
else
ignoreRendererSet = false;
} else {
Joel,
You are amazing! That worked great. What would you like for payment? You have gone above and beyond everytime i have a problem. I am also a photographer and if you want some art i would send you a framed photo of your choice or send you money. Here is the link to my photos. If you see something you like let me know:
https://www.flickr.com/photos/mr_kirkwood/with/53312488609
Cheers!
You are very generous, especially with such excellent photography. I am already well-rewarded though, so no need to feel like you owe me anything. Have a great weekend!