addPolygon
Adds a polygon area to a 3D map with customizable coordinates, colors, and elevation for visualizing boundaries or regions.
Instructions
在地图上添加多边形区域(面积、边界),返回 entityId
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| coordinates | Yes | 多边形外环坐标 [[lon, lat, height?], ...] | |
| color | No | 填充颜色(CSS 格式) | #3B82F6 |
| outlineColor | No | 描边颜色 | #FFFFFF |
| opacity | No | 填充透明度(0~1) | |
| extrudedHeight | No | 拉伸高度(米),可用于创建立体效果 | |
| clampToGround | No | 是否贴地 | |
| label | No | 多边形标注文本 |
Implementation Reference
- The core logic for creating a polygon in Cesium. It extracts parameters, validates coordinates, computes the centroid for labels, and adds the entity to the Cesium viewer.
export function addPolygon(viewer: Cesium.Viewer, params: AddPolygonParams): Cesium.Entity { const { coordinates, color = '#3B82F6', outlineColor = '#FFFFFF', opacity = 0.6, extrudedHeight, clampToGround = true, label } = params const fillColor = parseColor(color).withAlpha(opacity) const strokeColor = parseColor(outlineColor) const positions = coordinates.map(c => { validateCoordinate(c[0]!, c[1]!, c[2]) return Cesium.Cartesian3.fromDegrees(c[0]!, c[1]!, c[2] ?? 0) }) // 计算质心作为标签位置 const centroid = centroidOfCoords(coordinates.map(c => [c[0]!, c[1]!])) return viewer.entities.add({ position: (label && centroid) ? Cesium.Cartesian3.fromDegrees(centroid[0], centroid[1]) : undefined, polygon: { hierarchy: new Cesium.PolygonHierarchy(positions), material: fillColor, outline: true, outlineColor: strokeColor, outlineWidth: 1, heightReference: clampToGround ? Cesium.HeightReference.CLAMP_TO_GROUND : Cesium.HeightReference.NONE, extrudedHeight, }, label: label ? { text: label, font: '13px sans-serif', fillColor: Cesium.Color.WHITE, outlineColor: Cesium.Color.BLACK, outlineWidth: 2, style: Cesium.LabelStyle.FILL_AND_OUTLINE, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, disableDepthTestDistance: Number.POSITIVE_INFINITY, } : undefined, }) }