I am writing an app that can load different sets of user-preferences, and that can programatically change the basemap.
I have a function who's job it is to detect the changes needed for the map, and this is called whenever a user changes their layers, basemap, or when user-preferences are loaded. (the point i'm making is that there's no 'user has changed the basemap' event from the basemap widget when my alternate user-preferences are loaded).
I'm trying to detect that the basemap is not the same as the one in the preferences, and only change the basemap attribute when a difference is detected.
I'm storing a basemap.toJSON() in my user-preferences.
What can I use in that, to correlate to a basemap (that was created from basemap.fromJSON() ) ??
Solved! Go to Solution.
The watch will tell me when the actual map's Basemap is changed.
I'm trying to work out if what I store in my user-preferences data is the Basemap that's already in use on the map.
So you see, I'm trying to avoid changing the map's basemap. But I can't see what I can easily compare in my user-preferences to the on-screen Basemap.
Using the latest 4.10 api.
It appears the Basemap ID is generated uniquely each time I do a Basemap.fromJSON().
I wouldn't use the Title, that seems fragile.
The Basemap object itself cant be compared to what I store ... I store the Basemap.fromJSON().
I don't think I've tried comparing currentBasemap.toJSON() with my user-preferences storage...
The toJSON method returns a JSON-compatible object (good, it's not a string!), but that makes comparison tricky, because that's a 'deep attribute by attribute object comparison'. Is there no alternative? Would it be reliable - are the layer arrays stored in a predictable order?
FYI, it's working.
I see now that, with 'well known names', portal-items, and direct URL sources, that the matter of 'basemap identity' is not a trivial one.
Given that, this method of checking identity seems reasonable enough - I guess it would be nice to make it an official feature of the toJSON() methods, to help avoid running into regression issues.