เหตุการณ์จะเริ่มทำงานเมื่อผู้ใช้โต้ตอบกับวิดเจ็ตหรือการเปลี่ยนแปลงแบบเป็นโปรแกรมในวิดเจ็ต หากต้องการดำเนินการบางอย่างเมื่อเกิดเหตุการณ์ ให้ลงทะเบียนฟังก์ชัน Callback ในวิดเจ็ตด้วย onClick() (สำหรับ ui.Map หรือ ui.Button) หรือ onChange() (อื่นๆ ทั้งหมด) นอกจากนี้ คุณยังระบุ Callback ในคอนสตรัคเตอร์ได้ด้วย พารามิเตอร์สําหรับ Callback ของเหตุการณ์จะแตกต่างกันไปตามวิดเจ็ตและประเภทเหตุการณ์ เช่น ui.Textbox จะส่งค่าสตริงที่ป้อนในปัจจุบันไปยังฟังก์ชัน Callback ของกิจกรรมการคลิก ตรวจสอบเอกสารอ้างอิง API ในแท็บเอกสารเพื่อดูประเภทพารามิเตอร์ที่ส่งไปยังฟังก์ชัน Callback ของวิดเจ็ตแต่ละรายการ
ตัวอย่างต่อไปนี้แสดงเหตุการณ์หลายรายการที่เกิดจากการดำเนินการของผู้ใช้รายเดียวในการระบุรูปภาพที่จะแสดง เมื่อผู้ใช้เลือกรูปภาพ วิดเจ็ตการเลือกอีกรายการจะอัปเดตด้วยแบนด์ของรูปภาพและแสดงแบนด์แรกในแผนที่
ตัวแก้ไขโค้ด (JavaScript)
// Load some images. var dem = ee.Image('NASA/NASADEM_HGT/001'); var veg = ee.Image('NASA/VIIRS/002/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 โดยตั้งค่าแบนด์แรกเป็นค่าปัจจุบัน และฟังก์ชัน onChange ของ bandSelect จะเริ่มทำงานโดยอัตโนมัติ นอกจากนี้ โปรดทราบว่ามีการใช้ evaluate() เพื่อรับค่าของ ComputedObject ที่ bandNames() แสดงผลแบบอะซิงโครนัส ดูข้อมูลเพิ่มเติมได้ในส่วนเหตุการณ์แบบอะซิงโครนัส
Unlisten
เมธอด unlisten() ช่วยให้คุณนำฟังก์ชัน Callback ที่ลงทะเบียนในวิดเจ็ตออกได้ ซึ่งจะเป็นประโยชน์ในการป้องกันไม่ให้ทริกเกอร์เหตุการณ์ที่ควรเกิดขึ้นเพียงครั้งเดียวหรือภายใต้สถานการณ์บางอย่าง ค่าส่งกลับของ onClick() หรือ onChange() คือรหัสที่สามารถส่งผ่านไปยัง unlisten() เพื่อทำให้วิดเจ็ตหยุดเรียกใช้ฟังก์ชัน หากต้องการยกเลิกการลงทะเบียนเหตุการณ์ทั้งหมดหรือเหตุการณ์ประเภทที่เฉพาะเจาะจง ให้เรียกใช้ unlisten() โดยไม่มีอาร์กิวเมนต์หรืออาร์กิวเมนต์ประเภทเหตุการณ์ (เช่น 'click' หรือ 'change') ตามลำดับ ตัวอย่างต่อไปนี้แสดง 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() จะเริ่มส่งคำขอค่า และเมื่อค่าพร้อมแล้ว จะเรียกใช้ฟังก์ชัน Callback เพื่อดำเนินการบางอย่างกับผลลัพธ์ ตัวอย่างเช่น ลองพิจารณาแอปพลิเคชันเพื่อหาค่าเฉลี่ยของอนุกรมเวลา 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 กำลังประมวลผล ตัวอย่างนี้จะลงทะเบียนฟังก์ชัน Callback กับผลลัพธ์ โดยเฉพาะ computedValue.evaluate() เมื่อการคำนวณเสร็จสิ้น ระบบจะแสดงผลลัพธ์ ในระหว่างนี้ ระบบจะแสดงข้อความเพื่อระบุว่ากำลังดำเนินการคำนวณอยู่