0

I want to create a dropdown menu and when the user selects an item I want it to load that layer. This is my code

https://code.earthengine.google.com/dfbb128862bd8a3b0fb2139f5b0f5a28

var layers = {
    SRTM: ee.Image("CGIAR/SRTM90_V4"),
    Slope: ee.Terrain.slope(ee.Image("CGIAR/SRTM90_V4")),
}

  function changeLayers(x) {
  Map.layers().reset(layers[x.getValue()])
}


var s = ui.Select({
  placeholder:'Select Layer', 
  onChange: changeLayers})
Map.add(s)

What should I provide in the items menu so that it loads the layer from the dictionary. It expects an array though

1 Answer 1

1

If you're happy to use the same keys in the layers object as labels, you can use

items: ['SRTM', 'Slope']

To control the label, you can do this:

items: [
  {value: 'SRTM', label: 'Some label for SRTM'}, 
  {value: 'Slope', label: 'The Slope label'}
]

Altogether, might look like this:

// Define Layers as a dictionary
var layers = {
  SRTM: ee.Image("CGIAR/SRTM90_V4"),
  Slope: ee.Terrain.slope(ee.Image("CGIAR/SRTM90_V4")),
}

//create a function
function changeLayers(layerKey) {
  Map.layers().reset([layers[layerKey]])
}

// Make a selection ui.element that will update the layer
var s = ui.Select({
  placeholder: 'Select Layer',
  items: [
    {value: 'SRTM', label: 'Some label for SRTM'}, 
    {value: 'Slope', label: 'The Slope label'}
  ],
  onChange: changeLayers
})
Map.add(s)

https://code.earthengine.google.com/6199bcb31b98b53b14ab822f137d06c6

1
  • Thank you Daniel, You helped me with previous code as well, I was not able to understand the logic. Really means a lot :)
    – MS09
    Sep 20 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.