Zooming through a Loop of Features

1899
3
Jump to solution
02-08-2016 06:58 AM
TomLeMahieu
New Contributor II

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());

},

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Frequent Contributor

Tom LeMahieu

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.

JS Bin - Collaborative JavaScript Debugging

View solution in original post

0 Kudos
3 Replies
TimWitt2
MVP Alum

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

0 Kudos
KellyHutchins
Esri Frequent Contributor

Tom LeMahieu

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.

JS Bin - Collaborative JavaScript Debugging

0 Kudos
TomLeMahieu
New Contributor II

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();

0 Kudos