map.enableSnapping() - Async?

4885
6
Jump to solution
01-15-2014 09:56 AM
JamieSimko
New Contributor III
I am wondering if map.enableSnapping() has any asynchronous processing associated with it and if so are there any events I can listen to or a deferred promise that I can wait for?
I???ve done a bit of testing and it appears that this is the case.

If I do the following steps:

  1. enable snapping

  2. load in the feature layer (this waits for an ???onUpdateEnd??? event to fire which lets me know the layer is on the map)

  3. turn on the draw tool

Everything works fine however if I change the order to this:

  1. load in the feature layer (this waits for an ???onUpdateEnd??? event to fire which lets me know the layer is on the map)

  2. enable snapping

  3. turn on the draw tool

Snapping does not work on the first draw (it will work on subsequent ones).
0 Kudos
1 Solution

Accepted Solutions
JonathanUihlein
Esri Regular Contributor
Hi there,

Map API Ref:
https://developers.arcgis.com/en/javascript/jsapi/map-amd.html

"If snapping is enabled on the map using map.enableSnapping() this property provides access to the SnappingManager."

https://developers.arcgis.com/en/javascript/jsapi/snappingmanager-amd.html

I'm not sure there's an init or load event for the SnappingManager but you could probably just make sure the SnappingManager is accessable/exists before continuing on in your code.

View solution in original post

0 Kudos
6 Replies
JonathanUihlein
Esri Regular Contributor
Hi there,

Map API Ref:
https://developers.arcgis.com/en/javascript/jsapi/map-amd.html

"If snapping is enabled on the map using map.enableSnapping() this property provides access to the SnappingManager."

https://developers.arcgis.com/en/javascript/jsapi/snappingmanager-amd.html

I'm not sure there's an init or load event for the SnappingManager but you could probably just make sure the SnappingManager is accessable/exists before continuing on in your code.

View solution in original post

0 Kudos
drahoslavlacny
New Contributor

hi,

please, I have similar problem and I would need help to straighten my point of view.

I'm not sure there's an init or load event for the SnappingManager but you could probably just make sure the SnappingManager is accessable/exists before continuing on in your code.

Is it really recommended approach? In documentation is stated that after calling the function enableSnapping(), snappingManager is accessible through snappingManager property.

require([
 
"esri/map", ...
], function(Map, ... ) {
 
var map = new Map( ... );
 
var snappingManager = map.snappingManager();
 
...
});

So I would expect it to just be there. But unfortunately in reality it works or doesn't work that way. Snapping manager is returned inconsistently. Sometimes yes, sometimes no. Depending on the underlying structures and layers to be ready / loaded I guess.  

Or, on the other hand, if it ought to be async, the function enableSnapping() should require some callback function as an argument to be called in the moment the snapping manager is created and ready.

Or there should be an event on the map object to be listend and handled when snapping manager is ready and accessible.

Honestly, I can't imagine any other clean way of making sure the SnappingManager is accessable/exists. Do you? Any setInterval or setTimeout hacks? no...

please can somebody suggest the right approach?

Thank you very much in advance.

0 Kudos
JamieSimko
New Contributor III
Hey,

Thanks for the suggestion. I tested it out and for my use case it works. It seems strange to me that the snapping manager gets returned when you call map.enableSnapping(). Does anyone know why that would be when you would end up having access to it on the map object when it is truly available anyway?
0 Kudos
JonathanUihlein
Esri Regular Contributor
Awesome, glad it works for you!

Unfortunately, I am unable to answer your last question because I honestly have no idea.
I have not yet used the snappingManager and am not familiar with its inner workings.

However, I think it is safe to assume that there is a legitimate technical reason for having it this way... but again, I don't know what that reason is.

If someone else could explain, that would be great!

Lastly, make sure you mark this thread as 'answered' when you're completely satisfied =)
0 Kudos
JianHuang
Occasional Contributor III
map.enableSnapping() is a convenient method. You can achieve the same result by creating a new snappingManager.
0 Kudos
JamieSimko
New Contributor III
would creating a snapping manager and passing in my map as a parameter work any differently or would there still be a delay before snapping is ready?

I'm assuming that checking for the snapping manager on the map itself is probably the best way to know that it has been setup. Is this correct?

Thanks.
0 Kudos