on "basemap-change" not firing and getBasemap not working as expected

754
2
10-23-2013 08:12 AM
RobertWinterbottom
Occasional Contributor
Hi all,

  I am having some troubles with these two event listeners(onBasemapChange() and it's on equivalent "basemap-change") for the map as well as the map.getBasemap() function.

I have tried this in my own code and then also went to the basemap gallery sample and added these events/function to the sandbox to test there as well.

The sample I used is here: http://developers.arcgis.com/en/javascript/sandbox/sandbox.html?sample=widget_basemap

I then added the following code:
map.on("load",function(){
  map.on("basemap-change",function(){
    alert("Fired Basemap-change");
  });
});
        
basemapGallery.on("selection-change",function(){
  alert(map.getBasemap());
});


The "Fired Basemap-change" alert never fires even when I tried this
dojo.connect(map,"onBasemapChange",function(){
  alert("Fired Basemap-change")
})


The second alert will fire as it is inside the basemapGallery.on("selection-change") but the map.getBasemap() returns the same string every time, whatever I set the default basemap inside the map constructor to is the value this function always returns, I thought it was supposed to return the well-known name of the current basemap, is this normal for this function?

In the end I am building a custom dojo widget to sync 4 map objects together and am trying to add in a basemap syncing option as well so that when the user changes the basemap on the main map, it changes the other maps basemaps as well.  My thought was to get the well-known name of the current basemap from the main map and then loop through my array of other maps and just simply call map.setBasemap.  Is there another way to get this well-known basemap name?
0 Kudos
2 Replies
MattLane
Occasional Contributor II
If you are working with the basemap gallery, then you will have to monitor it's change events and use it's methods to get the current selected basemap.

basemapGallery.on('selection-change', function(){
  alert(basemapGallery.getSelected().title);
});


You'll probably want to return the id instead of the title, since that's what the basemapGallery.select() requires.
0 Kudos
RobertWinterbottom
Occasional Contributor
If you are working with the basemap gallery, then you will have to monitor it's change events and use it's methods to get the current selected basemap.

basemapGallery.on('selection-change', function(){
  alert(basemapGallery.getSelected().title);
});


You'll probably want to return the id instead of the title, since that's what the basemapGallery.select() requires.


Yea that's what I ended up doing, but since that returns the label or the id as "basemap_0" or whatever number the id has, I had to use a sort of lookup table to map the id/title to it's well-known name so that I can call map.setBasemap("gray") (or whatever the basemap name is) on the other map objects that are synced with the main one. Was kind of hoping I could call map.getBasemap() but it seems this is not tied to the basemap gallery so once you change basemaps, map.getBasemap() returns the incorrect name or use one of the aforementioned events which is supposed to return the well-known name but I can't get those to fire
0 Kudos