LabelCollection

new Cesium.LabelCollection(options)

可渲染标签的集合。标签是与视口对齐的文本,位于 3D 场景中。 每个标签可以具有不同的字体、颜色、缩放等。


示例标签


标签可以使用 LabelCollection#addLabelCollection#remove 方法从集合中添加和移除。
Performance:

为获得最佳性能,优先考虑少量集合,每个集合中有许多标签,而不是许多集合,每个集合中只有少量标签。 避免有些标签每帧更改而其他不变的集合;相反,创建一个或多个用于静态标签的集合, 以及一个或多个用于动态标签的集合。

Name Type Description
options object optional 具有以下属性的对象:
Name Type Default Description
modelMatrix Matrix4 Matrix4.IDENTITY optional 将每个标签从模型坐标转换为世界坐标的 4x4 变换矩阵。
debugShowBoundingVolume boolean false optional 仅用于调试。确定是否显示该基元的命令的边界球体。
scene Scene optional 必须传入,对于使用高度参考属性或将与地球进行深度测试的标签。
blendOption BlendOption BlendOption.OPAQUE_AND_TRANSLUCENT optional 标签的混合选项。默认 用于渲染不透明和半透明标签。但是,如果所有标签均完全不透明或全部完全透明, 将技术设置为 BlendOption.OPAQUE 或 BlendOption.TRANSLUCENT 可以提高性能达 2 倍。
show boolean true optional 决定集合中的标签是否显示。
Example:
// Create a label collection with two labels
const labels = scene.primitives.add(new Cesium.LabelCollection());
labels.add({
  position : new Cesium.Cartesian3(1.0, 2.0, 3.0),
  text : 'A label'
});
labels.add({
  position : new Cesium.Cartesian3(4.0, 5.0, 6.0),
  text : 'Another label'
});
Demo:
See:

Members

标签的混合选项。默认用于渲染不透明和半透明标签。 然而,如果所有标签都是完全不透明或全部完全透明, 将技术设置为 BlendOption.OPAQUE 或 BlendOption.TRANSLUCENT 可以提高 性能达 2 倍。
Default Value: BlendOption.OPAQUE_AND_TRANSLUCENT

debugShowBoundingVolume : boolean

此属性仅用于调试;不用于生产环境,也没有经过优化。

绘制基元中每个绘制命令的边界球体。

Default Value: false
返回此集合中的标签数量。通常与 LabelCollection#get 一起使用,以遍历集合中的所有标签。
将此集合中的每个标签从模型坐标转换为世界坐标的 4x4 变换矩阵。 当这是单位矩阵时,标签在世界坐标中绘制,即地球的 WGS84 坐标。 通过提供不同的变换矩阵(如 Transforms.eastNorthUpToFixedFrame 返回的矩阵)可以使用局部参考框架。
Default Value: Matrix4.IDENTITY
Example:
const center = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
labels.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(center);
labels.add({
  position : new Cesium.Cartesian3(0.0, 0.0, 0.0),
  text     : 'Center'
});
labels.add({
  position : new Cesium.Cartesian3(1000000.0, 0.0, 0.0),
  text     : 'East'
});
labels.add({
  position : new Cesium.Cartesian3(0.0, 1000000.0, 0.0),
  text     : 'North'
});
labels.add({
  position : new Cesium.Cartesian3(0.0, 0.0, 1000000.0),
  text     : 'Up'
});
决定此集合中的标签是否显示。
Default Value: true

Methods

创建并将具有指定初始属性的标签添加到集合中。 添加的标签将被返回,以便可以在以后修改或从集合中移除。
Performance:

调用 add 的预期时间复杂度为常量时间。然而,集合的顶点缓冲区会被重写;这个操作是 O(n),并且还会产生 CPU 到 GPU 的开销。为了获得最佳性能,尽可能在调用 update 之前添加尽可能多的公告牌。

Name Type Description
options Label.ConstructorOptions optional 描述标签属性的模板,如示例 1 所示。
Returns:
添加到集合中的标签。
Throws:
Examples:
// Example 1:  Add a label, specifying all the default values.
const l = labels.add({
  show : true,
  position : Cesium.Cartesian3.ZERO,
  text : '',
  font : '30px sans-serif',
  fillColor : Cesium.Color.WHITE,
  outlineColor : Cesium.Color.BLACK,
  outlineWidth : 1.0,
  showBackground : false,
  backgroundColor : new Cesium.Color(0.165, 0.165, 0.165, 0.8),
  backgroundPadding : new Cesium.Cartesian2(7, 5),
  style : Cesium.LabelStyle.FILL,
  pixelOffset : Cesium.Cartesian2.ZERO,
  eyeOffset : Cesium.Cartesian3.ZERO,
  horizontalOrigin : Cesium.HorizontalOrigin.LEFT,
  verticalOrigin : Cesium.VerticalOrigin.BASELINE,
  scale : 1.0,
  translucencyByDistance : undefined,
  pixelOffsetScaleByDistance : undefined,
  heightReference : HeightReference.NONE,
  distanceDisplayCondition : undefined
});
// Example 2:  Specify only the label's cartographic position,
// text, and font.
const l = labels.add({
  position : Cesium.Cartesian3.fromRadians(longitude, latitude, height),
  text : 'Hello World',
  font : '24px Helvetica',
});
See:

contains(label)boolean

检查该集合是否包含给定的标签。
Name Type Description
label Label 要检查的标签。
Returns:
如果该集合包含标签则返回 true,否则返回 false。
See:
销毁此对象持有的 WebGL 资源。销毁对象允许确定性地释放 WebGL 资源,而不是依赖垃圾收集器来销毁该对象。

一旦对象被销毁,不应再使用;调用除 isDestroyed 之外的任何函数将导致 DeveloperError 异常。因此, 像示例中那样将返回值(undefined)赋值给对象。
Throws:
Example:
labels = labels && labels.destroy();
See:
返回集合中指定索引处的标签。索引是从零开始的,并随着标签的添加而增加。移除一个标签会将其后的所有标签向左移动,改变它们的索引。此函数通常与 LabelCollection#length 一起使用,以遍历集合中的所有标签。
Performance:

预计时间复杂度为常量时间。如果从集合中移除标签并且 Scene#render 没有被调用,则会执行隐式的 O(n) 操作。

Name Type Description
index number 公告牌的零基索引。
Returns:
指定索引处的标签。
Throws:
Example:
// Toggle the show property of every label in the collection
const len = labels.length;
for (let i = 0; i < len; ++i) {
  const l = billboards.get(i);
  l.show = !l.show;
}
See:

isDestroyed()boolean

如果该对象已被销毁,则返回 true;否则返回 false。

如果该对象已被销毁,则不应使用;调用除 isDestroyed 之外的任何函数将导致 DeveloperError 异常。
Returns:
如果该对象已被销毁,则返回 true;否则返回 false。
See:

remove(label)boolean

从集合中移除一个标签。一旦被移除,标签将不再可用。
Performance:

调用 remove 的预期时间复杂度为常量时间。然而,集合的顶点缓冲区会被重写 - 这是一个 O(n) 的操作,并且会产生 CPU 到 GPU 的开销。为了获得最佳性能,尽可能在调用 update 之前移除尽可能多的标签。 如果您打算临时隐藏标签,通常调用 Label#show 会比移除和重新添加标签更有效。

Name Type Description
label Label 要移除的标签。
Returns:
如果标签被移除则返回 true;如果标签在集合中未找到,则返回 false
Throws:
Example:
const l = labels.add(...);
labels.remove(l);  // Returns true
See:
从集合中移除所有标签。
Performance:

O(n)。从集合中移除所有标签然后添加新的标签比完全创建一个新集合更有效。

Throws:
Example:
labels.add(...);
labels.add(...);
labels.removeAll();
See:
需要帮助?获得答案的最快方法是来自社区和团队 Cesium Forum.