addLabel
Add text labels to GeoJSON features by displaying specified attribute values on a 3D globe for spatial data visualization.
Instructions
为 GeoJSON 要素添加文本标注(显示属性值)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | GeoJSON FeatureCollection 对象 | |
| field | Yes | 用作标注文本的属性字段名(如 "name"、"population") | |
| style | No | 标注样式(font, fillColor, outlineColor, scale 等) |
Implementation Reference
- The core implementation of the addLabels logic which processes GeoJSON features and adds labels to the Cesium viewer.
export function addLabels( viewer: Cesium.Viewer, data: Record<string, unknown>, params: AddLabelParams, ): Cesium.Entity[] { const { field, style } = params const features = (data as any)?.features ?? [] const entities: Cesium.Entity[] = [] const font = style?.font ?? '14px sans-serif' const fillColor = style?.fillColor ? parseColor(style.fillColor) : Cesium.Color.WHITE const outlineColor = style?.outlineColor ? parseColor(style.outlineColor) : Cesium.Color.BLACK const outlineWidth = style?.outlineWidth ?? 2 const showBackground = style?.showBackground ?? false const backgroundColor = style?.backgroundColor ? parseColor(style.backgroundColor) : new Cesium.Color(0.1, 0.1, 0.1, 0.7) const pixelOffset = style?.pixelOffset ? new Cesium.Cartesian2(style.pixelOffset[0], style.pixelOffset[1]) : new Cesium.Cartesian2(0, -12) const scale = style?.scale ?? 1.0 for (const feature of features) { const props = feature?.properties ?? {} const text = props[field] if (text == null || text === '') continue const center = computeFeatureCentroid(feature) if (!center) continue const entity = viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(center[0], center[1]), label: { text: String(text), font, fillColor, outlineColor, outlineWidth, - The bridge-level method that wraps addLabels, manages entity data references, and handles layer association.
addLabel(params: AddLabelParams & { data?: Record<string, unknown> }): number { const data = params.data if (!data) return 0 // 优先检查是否已有同 dataRefId 的 GeoJSON 图层 → 将标注附加到现有实体上 if (params.dataRefId) { const existingRefs = this._layerManager.getCesiumRefs(params.dataRefId) if (existingRefs?.dataSource) { return this._attachLabelsToDataSource(existingRefs.dataSource, params) } } const entities = addLabels(this._viewer, data, params) - Type definition for AddLabelParams which defines the input structure for the addLabel tool.
export interface AddLabelParams { dataRefId?: string data?: Record<string, unknown> field: string style?: { font?: string fillColor?: string outlineColor?: string outlineWidth?: number backgroundColor?: string showBackground?: boolean pixelOffset?: [number, number] scale?: number } }