Catching an error loading Basemap to in order to load a backup Basemap

563
1
08-10-2010 01:27 PM
JacobBrozenick
New Contributor II
I'm putting together a system in which there is a case where a client machine needs to access the mapping portion of our web-app and still have full functionality w/o internet access.  We host the javascript api (currently on version 1.6) locally so that does not pose a problem.  Where we do have a problem is loading our basemap:
"http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"


What we would like to have happen is try to load the ArcGISTiledMapServiceLayer basemap and when/if it fails fall back to an ArcGISDynamicMapServiceLayer that we serve up locally.

Currently I have the tiled service layer wired up to an "onLoad" event which calls a function that creates the map adds the tiled layer as the base map if it has finished loading, then it proceeds to load the dynamic layer to the map.

I also have an "onError" wired up to the tiled layer so if it times out and fails to load it will still call the function to initialize the map but instead of adding the tiled layer as the base map it skips it and adds the dynamic layer instead.

The problem seems to be when the tiled layers "onError" is triggered the map that gets created never calls its "onLoad" function to initialize the rest of its functionality.

function init(){
[INDENT]tiledLayer = new esri.layers.ArcGISTiledMapServiceLayer("https://serverz.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer", {id:"stlayer"});
dojo.connect(tiledLayer, "onLoad", function(layer){
[INDENT]initMapLayers();
dojo.connect(tiledLayer, "onUpdate", hideLoading);[/INDENT]
});
dojo.connect(tiledLayer, "onError", function(err){
[INDENT]initMapLayers();[/INDENT]
}); [/INDENT]
}

function initMapLayers(){
[INDENT]esri.hide(dojo.byId("loadingImg"));
var ext = new esri.geometry.Extent(-15253161.87, 880554.17, -5958419.23, 7670608.66, new esri.SpatialReference({wkid: 102100}));
map = new esri.Map("map", {lods:lods, extent:ext});

dojo.connect(map, "onLoad", initFunctionality);
nav = new esri.toolbars.Navigation(map);
if(tiledLayer.loaded){
[INDENT]map.addLayer(tiledLayer);[/INDENT]
}

/*** DYNAMIC LAYERS ***/
var dynamicLayer = new esri.layers.ArcGISDynamicMapServiceLayer("url", {id:"urlid", opacity:0.4});
dojo.connect(dynamicLayer, "onUpdate", hideLoading);
map.addLayer(dynamicLayer);[/INDENT]
} 

dojo.addOnLoad(init);


In order to test the scenario where we have no internet connection i simply put in a url that does not exist for the tiledLayers constructor.

The tiledLayers "onError" event is getting triggered properly when it times out but the initFunctionality() function that is registered for the maps "onLoad" event does not seem to be getting triggered when initMapLayers() is called through the "onError" event of the tiledLayer.  When I use a correct URL and the tiledLayers "onLoad" event is triggered everything works fine.
1 Reply
HaroldBostic
Occasional Contributor II
Does the dynamic layer still get loaded?
0 Kudos