イベントは、ユーザーがウィジェットを操作するか、ウィジェットをプログラムで変更することで発生します。イベントが発生したときに何かを行うには、onClick()
(ui.Map
または ui.Button
の場合)または onChange()
(それ以外の場合)を使用して、ウィジェットにコールバック関数を登録します。コンストラクタでコールバックを指定することもできます。イベント コールバックのパラメータは、ウィジェットとイベントタイプによって異なります。たとえば、ui.Textbox
は、現在入力されている文字列値を「クリック」イベント コールバック関数に渡します。各ウィジェットのコールバック関数に渡されるパラメータの型については、[ドキュメント] タブの API リファレンスを確認してください。
次の例は、表示する画像を指定する単一のユーザー アクションから発生する複数のイベントを示しています。ユーザーが画像を選択すると、別の選択ウィジェットが画像のバンドで更新され、地図に最初のバンドが表示されます。
コードエディタ(JavaScript)
// Load some images. var dem = ee.Image('NASA/NASADEM_HGT/001'); var veg = ee.Image('NOAA/VIIRS/001/VNP13A1/2022_06_02') .select(['EVI', 'EVI2', 'NDVI']); // Make a drop-down menu of bands. var bandSelect = ui.Select({ placeholder: 'Select a band...', onChange: function(value) { var layer = ui.Map.Layer(imageSelect.getValue().select(value)); // Use set() instead of add() so the previous layer (if any) is overwritten. Map.layers().set(0, layer); } }); // Make a drop down menu of images. var imageSelect = ui.Select({ items: [ {label: 'NASADEM', value: dem}, {label: 'VIIRS Veg', value: veg} ], placeholder: 'Select an image...', onChange: function(value) { // Asynchronously get the list of band names. value.bandNames().evaluate(function(bands) { // Display the bands of the selected image. bandSelect.items().reset(bands); // Set the first band to the selected band. bandSelect.setValue(bandSelect.items().get(0)); }); } }); print(imageSelect); print(bandSelect);
ユーザーが画像を選択すると、画像のバンド名のリストが bandSelect
ウィジェットに読み込まれ、最初のバンドが現在の値に設定され、bandSelect
の onChange
関数が自動的に呼び出されます。また、evaluate()
を使用して、bandNames()
から返された ComputedObject
の値を非同期で取得していることにも注目してください。詳しくは、非同期イベントのセクションをご覧ください。
リスニングを停止
unlisten()
メソッドを使用すると、ウィジェットに登録されているコールバック関数を削除できます。これは、1 回だけ、または特定の状況でのみ発生する必要があるトリガー イベントを防ぐ場合に役立ちます。onClick()
または onChange()
の戻り値は、ウィジェットが関数の呼び出しを停止するために unlisten()
に渡すことができる ID です。すべてのイベントまたは特定のタイプのイベントを登録解除するには、引数なしで unlisten()
を呼び出すか、イベントタイプ('click'
や 'change'
など)を引数として指定して unlisten()
を呼び出します。次の例は、パネルの開閉を容易にするための unlisten()
を示しています。
コードエディタ(JavaScript)
// Create a panel, initially hidden. var panel = ui.Panel({ style: { width: '400px', shown: false }, widgets: [ ui.Label('Click on the map to collapse the settings panel.') ] }); // Create a button to unhide the panel. var button = ui.Button({ label: 'Open settings', onClick: function() { // Hide the button. button.style().set('shown', false); // Display the panel. panel.style().set('shown', true); // Temporarily make a map click hide the panel // and show the button. var listenerId = Map.onClick(function() { panel.style().set('shown', false); button.style().set('shown', true); // Once the panel is hidden, the map should not // try to close it by listening for clicks. Map.unlisten(listenerId); }); } }); // Add the button to the map and the panel to root. Map.add(button); ui.root.insert(0, panel);
unlisten()
は、パネルがすでに閉じているときにパネルを閉じるために、Map
がクリック イベントのリッスンを停止するために使用されています。
非同期イベント
ウィジェットで Earth Engine の結果(集計からの数値出力など)を使用する場合は、サーバーの値を取得する必要があります。(Earth Engine のクライアントとサーバーの詳細については、こちらのページをご覧ください)。値の計算中に UI 全体がハングしないようにするには、evaluate()
関数を使用して値を非同期的に取得します。evaluate()
関数は値のリクエストを開始し、値の準備ができたらコールバック関数を呼び出して結果を処理します。たとえば、ある地点での NDVI 時系列の平均値を取得するアプリケーションについて考えてみましょう。
コードエディタ(JavaScript)
// Load and display an NDVI image. var ndvi = ee.ImageCollection('LANDSAT/COMPOSITES/C02/T1_L2_8DAY_NDVI') .filterDate('2014-01-01', '2015-01-01'); var vis = {min: 0, max: 1, palette: ['99c199', '006400']}; Map.addLayer(ndvi.median(), vis, 'NDVI'); // Configure the map. Map.setCenter(-94.84497, 39.01918, 8); Map.style().set('cursor', 'crosshair'); // Create a panel and add it to the map. var inspector = ui.Panel([ui.Label('Click to get mean NDVI')]); Map.add(inspector); Map.onClick(function(coords) { // Show the loading label. inspector.widgets().set(0, ui.Label({ value: 'Loading...', style: {color: 'gray'} })); // Determine the mean NDVI, a long-running server operation. var point = ee.Geometry.Point(coords.lon, coords.lat); var meanNdvi = ndvi.reduce('mean'); var sample = meanNdvi.sample(point, 30); var computedValue = sample.first().get('NDVI_mean'); // Request the value from the server. computedValue.evaluate(function(result) { // When the server returns the value, show it. inspector.widgets().set(0, ui.Label({ value: 'Mean NDVI: ' + result.toFixed(2), })); }); });
ユーザーがこの地図上の点をタップすると、サーバーで reduceRegion()
呼び出しがトリガーされます。このオペレーションには時間がかかることがあります。Earth Engine の計算中にアプリケーションがブロックされないように、この例では、結果(computedValue.evaluate()
など)にコールバック関数を登録します。計算が完了すると、結果が表示されます。その間、計算が進行中であることを示すメッセージが表示されます。