Unable to switch BaseLayerPicker using javascript function

214 views
Skip to first unread message

artille...@gmail.com

unread,
Jun 8, 2019, 12:31:39 PM6/8/19
to cesium-dev
1. A concise explanation of the problem you're experiencing.
I have two imageryViewModels added to the "viewer" with baseLayerPick set to false. I was able to switch to either one of them by clicking on the drop-down icon. However, I am unable to switch from one to the other by clicking another button that calls a javascript function because I want to switch them on-the-fly through code.

I've been searching this forum and the stackoverflow for a while but I could not solve it. I found many examples show how to set and switch the default imagery when constructing a viewer, but not with customized ProviderViewModel. Please see my code below (I removed my access token). Thanks in advance!


2. A minimal code example. If you've found a bug, this helps us reproduce and repair it.

This is my HTML button: <button onclick="javascript:changeBaseLayer(1);">changeBaseLayer</button>


var imageryViewModels = [];

imageryViewModels.push(new Cesium.ProviderViewModel({
name: 'ArtilerySim Terrain',
iconUrl: Cesium.buildModuleUrl('Widgets/Images/ImageryProviders/mapboxSatellite.png'),
tooltip: 'Beautiful clear imagery',
creationFunction: function () {
return new Cesium.MapboxImageryProvider({
mapId: 'mapbox.satellite',
//Get your DigitalGlobe Maps API Access Token here: http://developer.digitalglobe.com/maps-api
accessToken: 'pk.',
credit: 'ArtilleryVision'
});
}
}));

imageryViewModels.push(new Cesium.ProviderViewModel({
name: 'ArtilerySim Street Maps',
iconUrl: Cesium.buildModuleUrl('Widgets/Images/ImageryProviders/mapboxStreets.png'),
tooltip: 'Beautiful clear imagery',
creationFunction: function () {
return new Cesium.MapboxImageryProvider({
mapId: 'mapbox.streets',
//Get your DigitalGlobe Maps API Access Token here: http://developer.digitalglobe.com/maps-api
accessToken: 'pk.',
credit: 'ArtilleryVision'
});
}
}));

var viewer = new Cesium.Viewer('cesiumContainer', {
baseLayerPicker: false,
terrainProvider: Cesium.createWorldTerrain(),
imageryProviderViewModels: imageryViewModels, //HQN: new with Mapbox
shouldAnimate: true,
homeButton: false,
animation: true,
timeline: true,
navigationHelpButton: false
});

var baseLayerPicker = new Cesium.BaseLayerPicker('baseLayerPickerContainer', {
globe: viewer.scene, // Yep, this is right
imageryProviderViewModels: imageryViewModels
});


// This is the javascript function that the button calls:
function changeBaseLayer(iIndex) {
console.log(imageryViewModels[1]);
baseLayerPicker.selectedImageryProviderViewModel = imageryViewModels[iIndex];
}


3. Context. Why do you need to do this? We might know a better way to accomplish your goal.
I want to switch my customized imageryViewModel on-the-fly using Javascript.


4. The Cesium version you're using, your operating system and browser.
I'm using version 1.55

Omar Shehata

unread,
Jun 12, 2019, 3:07:13 PM6/12/19
to cesium-dev
I think you need to change the selectedImagery property itself.

Try this Sandcastle, click the "Next" button to go to the next imagery provider in the list. Let me know if this does what you need! It it doesn't, it would help to create a Sandcastle link I can run directly to look into the issue.

H Nguyen

unread,
Jun 27, 2019, 9:14:26 PM6/27/19
to cesiu...@googlegroups.com
Omar,

I tried using your code, and it worked perfectly. Thank you so much for your help!

Cheers.
Henry

--
You received this message because you are subscribed to a topic in the Google Groups "cesium-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cesium-dev/DhPGPhlPSC8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cesium-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cesium-dev/d14e36fb-8478-4136-a1d0-f9eac38cf15e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

hoai...@gmail.com

unread,
Jun 30, 2019, 3:16:21 PM6/30/19
to cesium-dev
> To unsubscribe from this group and all its topics, send an email to cesiu...@googlegroups.com.
For people who may run into this problem, below is the code that I implemented using Omar's suggestion. It not only worked perfectly but also switch the drop-down-list to select the correct icon for that selected ImageryProvider that I set on-the-fly in code.

Thanks again to Omar for his big help!

var scene = viewer.scene;
var globe = viewer.scene.globe;
globe.depthTestAgainstTerrain = true;

var baseLayerPicker = new Cesium.BaseLayerPicker('baseLayerPickerContainer', {
// globe: Cesium.Ellipsoid.WGS84, // Psych! this is wrong.
globe: viewer.scene, // Yep, this is right
imageryProviderViewModels: imageryViewModels
});

function changeImageryProvider(index) {
var viewModel = baseLayerPicker.viewModel;

viewModel.selectedImagery = viewModel.imageryProviderViewModels[index];
}

Cheers.
Henry
Reply all
Reply to author
Forward
0 new messages