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'
});
Determines if labels in this collection will be shown.
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:
返回集合中指定索引处的标签。 索引从 0 开始 并随着标签的添加而增加。 删除标签会在 它向左移动,更改其索引。 此功能通常用于 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:
Need help? The fastest way to get answers is from the community and team on the Cesium Forum.