Identifying an existing Basemap, to avoid needless replacing.

572
7
Jump to solution
01-09-2019 03:42 AM
NathanSummers1
New Contributor III

Situation:

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

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

I am not sure about the predictability of the order. I am not aware of any alternative,

View solution in original post

0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus

Nathan,

   Can you not just add a watch to the maps basemap property (assuming 4.x API since you did not specify)?

0 Kudos
NathanSummers1
New Contributor III

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.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Nathan,

   So the maps basemap property give you a string of the basemap name or a basemap object... Can you not use that to compare?

0 Kudos
NathanSummers1
New Contributor III

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?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

I am not sure about the predictability of the order. I am not aware of any alternative,

0 Kudos
NathanSummers1
New Contributor III

Thanks so far Robert - I'll use the 'deep-equals' library and see if I get good results comparing the JSON-ready objects.

0 Kudos
NathanSummers1
New Contributor III

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.

Many thanks.

0 Kudos