After doing a setLayerDefinitions on a ArcGISDynamicMapServiceLayer layer. How do I zoom to results?
What version of ArcGIS Server are you using? If you are using one that supports queryExtent that that is the best way.
ArcGIS Server is 10.5.1.
queryExtent I believe is only available with a FeatureLayer, not a ArcGISDynamicMapServiceLayer. Hence why I'm asking the question about how to do it with a ArcGISDynamicMapServiceLayer.
Garey,
Any query (like queryExtent) is going to be specific to a individual layer in a map service just like a definition query would be too. Unfortunately applying a definition expression does not alter the extent returned for that map service so there is no easy way to just zoom to the extent of the set definition expression.
Okay figured this out. no way to do it directly with a ArcGISDynamicMapServiceLayer, so you need to create a temporary FeatureLayer and get the extent of it after applying the same definition expression that was applied to the ArcGISDynamicMapServiceLayer. Sorry about the formatting. When you cut and paste it strips the tabs.
function WDsetDefinitionExpressionDone(results) {
if (results.error) {
alert("Error occured getting WD trade area. "+results.error.message);
console.log("results.error",results.error);
} else {try {
// Hide layer while we calc renderer and re-display
wdLayer.hide();
// Zoom to the layers objects
var filterDefinition = wdLayer.layerDefinitions[1];
var tempLayer = new esri.layers.FeatureLayer(
"https://mktserv5.goodyear.com:6443/arcgis/rest/services/MarketAnalysis/MarketAnalysis_WD_TradeAreas/...",
{
id: "Temp Feature Layer"
,outFields: ["*"]
}
);
var queryObject = new query();
queryObject.where = filterDefinition;
tempLayer.queryExtent(queryObject,function(response) {
// queryExtent worked.
var extent = response.extent;
if (extent) {
extent.expand(2.0);
map.setExtent(extent).then(function (response) {
// Color code the layer
var promise = smartMapping.createClassedColorRenderer({
layer: tempLayer
,field: "MARKETANALYSIS.WD_SUBGEO_SALES.WD_NET_PURCHASES_UNITS"
,showOthers: true
,basemap: map.getBasemap()
,classificationMethod: "natural-breaks"
,numClasses: 6
}).then(function (response) {
// Color coding completed now display it
var layerDrawingOptions = [];
var layerDrawingOption1 = new esri.layers.LayerDrawingOptions();
layerDrawingOption1.renderer = response.renderer;
layerDrawingOptions[1] = layerDrawingOption1;
wdLayer.setLayerDrawingOptions(layerDrawingOptions,true);
// Have to toggle layer on and off to make sure colors always display.
wdLayer.hide();
wdLayer.show();
},function (error) {
console.log("WD Coloring Error",error);
alert("WD Coloring Error: "+error);
});
});
} else {
console.log("Error getting extent of trade area from tempLayer",tempLayer);
alert("Error getting extent of trade area");
}
}, function(error){
// Error querying extent
console.log("tempLayer.queryExtent error",error);
alert("tempLayer.queryExtent error");
});// // Setup fuction to run after filter is applied
// dojo.on.once(tempLayer, "update-end", zoomTempLayer);
// // Apply the filter
// tempLayer.setDefinitionExpression(filterDefinition);
} catch ($err) {
console.log("Error color coding WD Trade Area", $err);
alert("Error color coding WD Trade Area: $err");
}
}
}
// wdLayer.setDefinitionExpression("WKUNNR='" + wkunnr + "'")
var definitions = [];
definitions[1] = "WKUNNR='" + wkunnr + "'";
wdLayer.setLayerDefinitions(definitions);
dojo.on.once(wdLayer, "update-end", WDsetDefinitionExpressionDone);
// "update-end" won't happen unless layer is displayed.
map.addLayer(wdLayer);
Garey,
Honestly that is overkill. Just use a queryTask and query to get the extent of the specific layer in the ArcGISDynamicMapServiceLayer that you are applying the definition to.
Okay. Have an example? Didn't think you could get to sub layers within a ArcGISDynamicMapServiceLayer as a featureLayer to do a queryExtent.
Garey,
Sure here is a snippet
//be sure to require QueryTask
//require(["esri/tasks/QueryTask"], function(QueryTask) {
// Zoom to the layers objects
var queryTask = new QueryTask("https://mktserv5.goodyear.com:6443/arcgis/rest/services/MarketAnalysis/MarketAnalysis_WD_TradeAreas/...");
var queryObject = new query();
queryObject.where = wdLayer.layerDefinitions[1];
queryTask.queryExtent(queryObject, function(response) {
:
Tried something similar, but the extent would be wrong if I didn't wait for the "update-end" event. Otherwise it would zoom to part of the shapes and leave others off the screen.
Garey,
That was because you were relying on a layer to get the definition query applied. Using a QueryTask is very different.