eliprandi

persist basemap across maps within browser session

Discussion created by eliprandi on Mar 4, 2013
Latest reply on Mar 5, 2013 by eliprandi
Hi,
We are building an app in which our users would like to select a particular basemap from the gallery. Our application consists of many pages showing various assets. We would like to persist the basemap for the user's current session. This seems to be pretty simple if we can store the basemap id the user last selected. This works fine using the "onSelectionChange" event of the BasemapGallery.

However, we are having a terrible trying to set the basemap programmatically when the new page loads. We read the cookie properly, but we can't seem to figure out the right time to call select(id) on the BasemapGallery. It seems that the BasemapGallery always fires once with the first map selection. Yes, we can handle this, but the result is very hacky in our mind. Is there a way to specify which basemap to use after startup()?
Here is our latest attempt:
dojo.require("esri.map");
dojo.require("esri.dijit.BasemapGallery");
dojo.require("dojo/cookie");

var map;

function init() {
    map = new esri.Map("mapDiv", {
        center: [19.461, 53.914],
        zoom: 5
    });
    var basemapGallery = new esri.dijit.BasemapGallery({
        showArcGISBasemaps: true,
        bingMapsKey: 'afdafadsfdsafsdfasdfdsfdsf_afdfafdasfdsaf',
        map: map
    }, "basemapGallery");

    basemapGallery.startup();

    dojo.connect(basemapGallery, "onError", function (msg) { console.log(msg); });

    dojo.connect(basemapGallery, "onLoad", function() {
        var basemapId = dojo.cookie("userSelectedBaseMap");
        if (basemapId != null) {
            basemapGallery.select(basemapId);
        }
        dojo.connect(basemapGallery, "onSelectionChange", function () {
            var selected = basemapGallery.getSelected();
            dojo.cookie("userSelectedBaseMap", selected.id);
        });
    });
            

}
dojo.ready(init);

Is there a recommended way to achieve what we are trying to do? how about a recommendation on when to programmatically set the basemap selection?

Thanks for any help,

Eric

Outcomes