Good afternoon everybody,
In my webapp I use the map centerAndZoom method Map | API Reference | ArcGIS API for JavaScript which will return a deferred.
I haven't worked with deferred before and was wondering how I can continue with my code once the centerAndZoom has finished.
Would it look something like this or can somebody (probably Robert) give me an example?
map.centerAndZoom(allPoints[0].geometry,8).then(function(){
//continuation of the code
});
Thanks,
Tim
Solved! Go to Solution.
Tim,
That is it exactly.
Robert, I tried to add a flash point marker graphic in both ways when !this.map.extent.contains(zoomExtent):
1.
this.map.centerAt(zoomExtent).then(DoFlash());
2.
new Promise(function(resolve, reject) {
this.map.centerAt(zoomExtent);
}).then(DoFlash());
I got error message 'Uncaught lang.hitch: scope["setStroke"] is null (scope="[object Window]")'. But no problem DoFlash() if the map doesn't need to be re-centered.
Please help, thanks!
Tong,
Because you have this.map I have to assume that your issue is a scope issue and your code should look like:
this.map.centerAt(zoomExtent).then(lang.hitch(this, DoFlash));
Robert,
Thanks for your advice. I tried your suggestion of "this.map.centerAt(zoomExtent).then(lang.hitch(this, DoFlash()));"
Then I tried "map.centerAt(zoomExtent).then(lang.hitch(this, DoFlash()));"
I still got the same error message.
Basically, I need to make sure DoFlash() happens after the map is re-centered. But when I put "alert" at the beginning of DoFlash(), the map exent has not changed yet.
Tong,
I updated my code suggestion.
When you have
this.map.centerAt(zoomExtent).then(lang.hitch(this, DoFlash()));
the DoFlash() means call the function immediately when the code is parsed.
You need:
this.map.centerAt(zoomExtent).then(lang.hitch(this, DoFlash));
Thank you so much! It works now.
I would probably try bunch of other things before even thinking about there is difference between these two.
Yeah, centerAndZoom returns a Promise, which comes from the Deferred. This means you can use the "then()" method as your sample shows to continue your code.
Thank you, since I've never dealt with deferred, I figured I ask to make sure I understand it.
Well, I tried to add a flash point marker graphic in both ways when !this.map.extent.contains(zoomExtent):
1.
this.map.centerAt(zoomExtent).then(DoFlash());
2.
new Promise(function(resolve, reject) {
this.map.centerAt(zoomExtent);
}).then(DoFlash());
I got error message 'Uncaught lang.hitch: scope["setStroke"] is null (scope="[object Window]")'. But no problem DoFlash() if the map doesn't need to be re-centered.
Please help.
Thanks!