Skip to main content
Glama

updateLayerStyle

Modify existing layer visual properties including colors, transparency, labeling styles, and stroke widths for 3D globe visualization in CesiumJS.

Instructions

修改已有图层的样式(颜色、透明度、标注样式等)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
layerIdYes图层ID
labelStyleNo标注样式(font, fillColor, outlineColor, outlineWidth, scale 等)
layerStyleNo图层样式(color, opacity, strokeWidth, pointSize)

Implementation Reference

  • The implementation of updateLayerStyle which modifies Cesium entities (labels, polyline, polygon, billboard, point) based on the provided style parameters.
    updateLayerStyle(params: UpdateLayerStyleParams): boolean {
      const layer = this._layers.find(l => l.id === params.layerId)
      if (!layer) return false
    
      const refs = this._cesiumRefs.get(params.layerId)
    
      // 标注图层样式更新
      const ls = params.labelStyle
      if (ls && refs?.labelEntities) {
        const entities: Cesium.Entity[] = refs.labelEntities
        for (const entity of entities) {
          if (!entity.label) continue
          if (ls.font || ls.fontSize) {
            const fontSize = ls.fontSize ?? 14
            entity.label.font = new Cesium.ConstantProperty(ls.font ?? `${fontSize}px sans-serif`)
          }
          if (ls.fillColor) {
            entity.label.fillColor = new Cesium.ConstantProperty(
              parseColor(ls.fillColor)
            )
          }
          if (ls.outlineColor) {
            entity.label.outlineColor = new Cesium.ConstantProperty(
              parseColor(ls.outlineColor)
            )
          }
          if (ls.outlineWidth !== undefined) {
            entity.label.outlineWidth = new Cesium.ConstantProperty(ls.outlineWidth)
          }
          if (ls.scale !== undefined) {
            entity.label.scale = new Cesium.ConstantProperty(ls.scale)
          }
          if (ls.showBackground !== undefined) {
            entity.label.showBackground = new Cesium.ConstantProperty(ls.showBackground)
          }
          if (ls.backgroundColor) {
            entity.label.backgroundColor = new Cesium.ConstantProperty(
              parseColor(ls.backgroundColor)
            )
          }
          if (ls.pixelOffset) {
            entity.label.pixelOffset = new Cesium.ConstantProperty(
              new Cesium.Cartesian2(ls.pixelOffset[0], ls.pixelOffset[1])
            )
          }
        }
        if (ls.fillColor) layer.color = ls.fillColor
        return true
      }
    
      // GeoJSON 图层样式更新
      const gs = params.layerStyle
      if (gs && refs?.dataSource) {
        const ds = refs.dataSource
        const entities = ds.entities.values
        const color = gs.color ? parseColor(gs.color) : null
        const opacity = gs.opacity ?? 0.6
        for (const entity of entities) {
          if (entity.polyline) {
            if (color) entity.polyline.material = new Cesium.ColorMaterialProperty(color.withAlpha(opacity))
            if (gs.strokeWidth !== undefined) entity.polyline.width = new Cesium.ConstantProperty(gs.strokeWidth)
          }
          if (entity.polygon) {
            if (color) {
              entity.polygon.material = new Cesium.ColorMaterialProperty(color.withAlpha(opacity * 0.4))
              entity.polygon.outlineColor = new Cesium.ConstantProperty(color)
            }
          }
          if (entity.billboard) {
            const newSize = gs.pointSize ?? undefined
            const newColor = color ?? undefined
            if (newColor || newSize) {
              const cssCol = gs.color ?? layer.color ?? '#3B82F6'
              const sz = newSize ?? 10
              entity.billboard.image = new Cesium.ConstantProperty(createCircleImage(sz * 2, cssCol, opacity)) as any
              entity.billboard.width = new Cesium.ConstantProperty(sz * 2) as any
              entity.billboard.height = new Cesium.ConstantProperty(sz * 2) as any
            }
          }
          if (entity.point) {
            if (color) entity.point.color = new Cesium.ConstantProperty(color.withAlpha(opacity))
            if (gs.pointSize !== undefined) entity.point.pixelSize = new Cesium.ConstantProperty(gs.pointSize)
          }
        }
        if (gs.color) layer.color = gs.color
        return true
      }
    
      return false

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