Google Earth Engine – How to Display Regions from a List Efficiently

arraydropdownlistgoogle-earth-enginepanel

I have generated a list with States of India but getting the following error while trying to display a region on the map.

Line 11: centerObject() requires a Geometry or Element. Got: Karnataka

'Karnataka' is the State which I tried to display.

Code used is as follows :

// Let's add a dropdown with the location names using aggregate_array()
var items = India.aggregate_array('NAME_1');
items.evaluate(function(List) {
  dropdown.items().reset(List),
  dropdown.setPlaceholder('Select Location');
});
var dropdown =ui.Select({
  placeholder: 'please wait...',
  onChange : function(key){
    Map.clear();
    Map.centerObject(key,7);
    Map.addLayer(key,{},dropdown.getValue());
  }
});

//Main Panel
var panel = ui.Panel({
  widgets: [dropdown],
  style: {backgroundColor: 'white'},
  layout: ui.Panel.Layout.flow('vertical')
});

//displaying the main panel
ui.root.insert(1,panel);

where 'India' is the imported asset and 'NAME_1' is the column with the names of various States.

How can I display the selected State on the map ? I believe the error is in Map.centerObject and Map.addLayer lines.

Best Answer

The onChange function gets a string when the value changes (you've called that value 'key'). You need to use that string to filter your collection instead of just trying to display the string. Your code isn't complete, but assuming that India is a FeatureCollection from something like a shapefile:

var dropdown = ui.Select({
  placeholder: 'please wait...',
  onChange : function(key){
    var region = India.filter(ee.Filter.eq('NAME1', key))
    Map.layers().reset()
    Map.centerObject(region,7);
    Map.addLayer(region,{},key);
  }
});
Related Question