I am trying to step through a set of selected features, zoom to the extent of the feature, do something with it and then go to the next feature and do the same. I can't seem to trap the end of the zoom no matter what I do. Does anybody have code that does this successfully. It works fine for one. It just doesn't work when I loop through a set of features. Here's what I've tried:
printSingleParcelFeature: function (feature, callback) {
var self = this;
self.zoomSelectedOne(feature, function() {
callback();
});
},
zoomSelectedOne: function(feature, callback) {
var gExt;
var selectedExt;
//Also tried 'zoom-end'. That never works.
var mapZoomChange = this.map.on('extent-change', zoomHandler);
function zoomHandler() {
console.info("Extent Change");
setTimeout(lang.hitch(this, function () {
mapZoomChange.remove();
callback();
}), 1000);
}
gExt = feature.geometry.getExtent();
if (gExt) {
selectedExt = gExt.expand(this.zoomSelectExpand,true);
this.map.setExtent(selectedExt);
console.info("Set Extent: " + feature.attributes["PIN"]);
}
},
My console results are basically this:
I also tried this, but again the extent only changes for the last one:
zoomSelectedOne: function (feature, callback) {
var gExt;
var selectedExt;
gExt = feature.geometry.getExtent();
if (gExt) {
selectedExt = gExt.expand(this.zoomSelectExpand,true);
this.map.setExtent(selectedExt).then(callback());
}
},
Solved! Go to Solution.
One option would be to wait until setExtent is complete then do what you need to do then navigate to the next features extent. Here's a jsbin that shows zooming to a feature, generating a printout, then zooming to the next feature.
Hey Tom,
Instead of waiting for the "extent-change" event to finish, use the featurelayer "update-end" event?
FeatureLayer | API Reference | ArcGIS API for JavaScript
Even though the extent is changed, i think your feature layer isn't ready yet?
Tim
One option would be to wait until setExtent is complete then do what you need to do then navigate to the next features extent. Here's a jsbin that shows zooming to a feature, generating a printout, then zooming to the next feature.
Kelly & Tim,
Thanks for your responses. Both helped get me on the right track. It ended up that my problem was how I was stepping through the selected features. It wasn't getting to the callback until it had stepped through all the features.
Not working:
Working:
for (var i = 0; i < numSelected; i++) {
feature = features;
self.printSoilErosionFeature(feature, function() {
//nothing
});
}
callback();
count = 0;
function processFeatures() {
if (features.length > count) {
var feature = features[count];
self.printSingleParcelFeature(feature, processFeatures)
count++;
} else {
callback();
}
}
processFeatures();