// 创建点状符号图层和结果图层 const graphicsLayer = new GraphicsLayer(); const resultsLayer = new GraphicsLayer(); map.addMany([graphicsLayer, resultsLayer]); // 创建点数组 const pointArray = []; for (let i = 0; i < points.length; i++) { pointArray.push([points[i][0], points[i][1]]); } // 创建多点对象 const multipoint = new Multipoint({ points: pointArray }); // 创建简单点状符号 const simpleMarkerSymbol = { type: "simple-marker", color: [226, 119, 40], outline: { color: [255, 255, 255], width: 1 } }; // 创建点状符号图形 const multipointGraphics = multipoint.points.map((point) => { return new Graphic({ geometry: { type: "point", longitude: point[0], latitude: point[1] }, symbol: simpleMarkerSymbol }); }); graphicsLayer.addMany(multipointGraphics); view.ui.add(document.getElementById("controls"), "down-right"); document.getElementById("earthquake").addEventListener("click", visual); document.getElementById("buffer").addEventListener("click", createBuffers); document.getElementById("reset").addEventListener("click", resetGraphics); document.getElementById("heatmap").addEventListener("click", creatheatmap); document.getElementById("colse").addEventListener("click", closeheatmap); // 用于存储缓冲区图形的数组 let bufferGraphics = []; // 创建缓冲区图形 function createBuffers() { // 如果已经创建了缓冲区,则直接返回 if (bufferGraphics.length > 0) { return; } // 遍历多点图形列表,为每个点创建缓冲区图形 bufferGraphics = multipointGraphics.map((pointGraphic) => { const buffer = geometryEngine.geodesicBuffer( pointGraphic.geometry, 10, "kilometers" ); return new Graphic({ geometry: buffer, symbol: { type: "simple-fill", color: [227, 139, 79, 0.5], outline: { color: [255, 255, 255, 255] }, }, }); }); // 将缓冲区图形添加到结果图层中 resultsLayer.addMany(bufferGraphics); } function resetGraphics() { resultsLayer.removeAll(); bufferGraphics = []; }计算resultLayer缓冲后的面积,写到一个单独的函数里,并把结果输出到textbox里
时间: 2024-03-18 09:38:30 浏览: 126
openLayer-图层高亮源码
你可以在`createBuffers()`函数中,添加计算缓冲区面积的代码,并将结果输出到textbox中。代码如下:
```javascript
function createBuffers() {
// 如果已经创建了缓冲区,则直接返回
if (bufferGraphics.length > 0) {
return;
}
// 遍历多点图形列表,为每个点创建缓冲区图形
bufferGraphics = multipointGraphics.map((pointGraphic) => {
const buffer = geometryEngine.geodesicBuffer(
pointGraphic.geometry,
10,
"kilometers"
);
return new Graphic({
geometry: buffer,
symbol: {
type: "simple-fill",
color: [227, 139, 79, 0.5],
outline: {
color: [255, 255, 255, 255]
},
},
});
});
// 计算缓冲区面积并输出到textbox中
const totalArea = bufferGraphics.reduce((acc, curr) => {
return acc + geometryEngine.planarArea(curr.geometry, "square-kilometers");
}, 0);
document.getElementById("result").value = `缓冲区面积为:${totalArea.toFixed(2)} 平方千米`;
// 将缓冲区图形添加到结果图层中
resultsLayer.addMany(bufferGraphics);
}
```
在代码中,我们使用了`reduce()`方法来遍历缓冲区图形数组,并计算它们的面积总和。然后将结果输出到名为`result`的textbox中。注意,我们使用了`geometryEngine.planarArea()`方法来计算平面面积,因为缓冲区是平面图形,不需要考虑地球曲率的影响。
阅读全文