Skip to main content
Glama

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
NameRequiredDescriptionDefault
coordinatesYes多边形外环坐标 [[lon, lat, height?], ...]
colorNo填充颜色(CSS 格式)#3B82F6
outlineColorNo描边颜色#FFFFFF
opacityNo填充透明度(0~1)
extrudedHeightNo拉伸高度(米),可用于创建立体效果
clampToGroundNo是否贴地
labelNo多边形标注文本

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,
      })
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/gaopengbin/cesium-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server