LabelCollection

new Cesium.LabelCollection(options)

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


示例标签


Labels are added and removed from the collection using LabelCollection#add and LabelCollection#remove.
Performance:

For best performance, prefer a few collections, each with many labels, to many collections with only a few labels each. Avoid having collections where some labels change every frame and others do not; instead, create one or more collections for static labels, and one or more collections for dynamic labels.

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 确定集合中的标签是否显示。
coarseDepthTestDistance number optional 从相机的距离,超过该距离后,标签将根据地球椭球体的近似值进行深度测试,而不是根据完整的地球深度缓冲区。如果未指定,默认值将相对于 Ellipsoid.default 的值确定。
threePointDepthTestDistance number optional 从相机的距离,在此范围内,具有 Label#heightReference 值为 HeightReference.CLAMP_TO_GROUNDHeightReference.CLAMP_TO_TERRAIN 的标签将根据三个关键点进行深度测试。这确保了如果标签的任何关键点可见,整个标签都可见。如果未指定,默认值将相对于 Ellipsoid.default 的值确定。
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

coarseDepthTestDistance : number

从相机的距离,超过该距离后,标签将根据地球椭球体的近似值进行深度测试, 而不是根据完整的地球深度缓冲区。设置为 0 时, 始终应用近似深度测试。设置为 Number.POSITIVE_INFINITY 时, 从不应用近似深度测试。

此设置仅当标签的 Label#disableDepthTestDistance 值本应允许深度测试时才生效——即,从相机到标签的距离小于 标签的 Label#disableDepthTestDistance 值。

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

threePointDepthTestDistance : number

从相机的距离,在此范围内,具有 Label#heightReference 值为 HeightReference.CLAMP_TO_GROUNDHeightReference.CLAMP_TO_TERRAIN 的标签将根据三个关键点进行深度测试。 这确保了如果标签的任何关键点可见,整个标签都可见。设置为 0 时, 将禁用此功能,并且位于地形后面的标签部分会被裁剪。

此设置仅当标签的 Label#disableDepthTestDistance 值本应允许深度测试时才生效——即,从相机到标签的距离小于 标签的 Label#disableDepthTestDistance 值。
See:

Methods

使用指定的初始属性创建并向集合中添加标签。 返回添加的标签,以便稍后修改或从集合中移除。
Performance:

Calling add is expected constant time. However, the collection's vertex buffer is rewritten; this operations is O(n) and also incurs CPU to GPU overhead. For best performance, add as many billboards as possible before calling 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:

Expected constant time. If labels were removed from the collection and Scene#render was not called, an implicit O(n) operation is performed.

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:
如果此对象已被销毁则返回 true,否则返回 false。

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

remove(label)boolean

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

Calling remove is expected constant time. However, the collection's vertex buffer is rewritten - an O(n) operation that also incurs CPU to GPU overhead. For best performance, remove as many labels as possible before calling update. If you intend to temporarily hide a label, it is usually more efficient to call Label#show instead of removing and re-adding the label.

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

O(n). It is more efficient to remove all the labels from a collection and then add new ones than to create a new collection entirely.

Throws:
Example:
labels.add(...);
labels.add(...);
labels.removeAll();
See:
需要帮助?获取答案的最快方式是在 Cesium 论坛 上向社区和团队提问。