How to swap imagery providers on error

hi all,

Is there an easy way to swap my imagery provider for a different imagery provider if the first throws an error?

E.G.: My map loads with imagery from Bing; if for whatever reason it can’t access imagery from Bing (I typed in my key wrong, whatever), I’d like it to access imagery from a USGS server.

You can use the imageryProvider’s readyPromise to listen for whether it was successfully created, and if not, you can remove it and add a different layer. Luckily there is one failing Bing layer in Sandcastle we can use to test:

var imageryProvider = new Cesium.BingMapsImageryProvider({

url : ‘https://dev.virtualearth.net’,

mapStyle : Cesium.BingMapsStyle.ROAD

});

var viewer = new Cesium.Viewer(‘cesiumContainer’, {

imageryProvider: imageryProvider

});

imageryProvider.readyPromise.then(function() {

console.log(“Success!”);

})

.otherwise(function(error) {

console.log(error);

// Remove the first layer:

viewer.imageryLayers.remove(viewer.imageryLayers.get(0))

// Add a different one:

viewer.imageryLayers.addImageryProvider(new Cesium.IonImageryProvider({ assetId: 3845 }));

});

``

Live Sandcastle link.

Thanks Omar, you’re a lifesaver.