map.centerAndZoom deferred

4047
9
Jump to solution
01-28-2016 10:13 AM
TimWitt2
MVP Alum

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

1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Tim,

   That is it exactly.

View solution in original post

9 Replies
RobertScheitlin__GISP
MVP Emeritus

Tim,

   That is it exactly.

TONGSUN
New Contributor

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!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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));‍‍
0 Kudos
TONGSUN
New Contributor

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.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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));
0 Kudos
TONGSUN
New Contributor

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. 

0 Kudos
ReneRubalcava
Frequent Contributor

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.

TimWitt2
MVP Alum

Thank you, since I've never dealt with deferred, I figured I ask to make sure I understand it.

0 Kudos
TONGSUN
New Contributor

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!

0 Kudos