
示例标签
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
包含以下属性的对象:
|
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 : BlendOption
-
Default Value:
BlendOption.OPAQUE_AND_TRANSLUCENT
0 时,
始终应用近似深度测试。设置为 Number.POSITIVE_INFINITY 时,
从不应用近似深度测试。
此设置仅当标签的
Label#disableDepthTestDistance 值本应允许深度测试时才生效——即,从相机到标签的距离小于
标签的 Label#disableDepthTestDistance 值。
绘制图元中每个绘制命令的包围球。
-
Default Value:
false
LabelCollection#get 用于遍历集合中的所有标签。
modelMatrix : Matrix4
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
Label#heightReference 值为
HeightReference.CLAMP_TO_GROUND 或 HeightReference.CLAMP_TO_TERRAIN 的标签将根据三个关键点进行深度测试。
这确保了如果标签的任何关键点可见,整个标签都可见。设置为 0 时,
将禁用此功能,并且位于地形后面的标签部分会被裁剪。
此设置仅当标签的
Label#disableDepthTestDistance 值本应允许深度测试时才生效——即,从相机到标签的距离小于
标签的 Label#disableDepthTestDistance 值。
Methods
add(options) → Label
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:
-
DeveloperError : 此对象已被销毁,即调用了 destroy()。
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:
| Name | Type | Description |
|---|---|---|
label |
Label | 要检查的标签。 |
Returns:
See:
一旦对象被销毁,就不应再使用它;调用除
isDestroyed 之外的任何函数都会导致 DeveloperError 异常。因此,
应将返回值(undefined)赋给该对象,如示例所示。
Throws:
-
DeveloperError : 此对象已被销毁,即调用了 destroy()。
Example:
labels = labels && labels.destroy();
See:
get(index) → Label
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:
-
DeveloperError : 此对象已被销毁,即调用了 destroy()。
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:
Returns:
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:
-
DeveloperError : 此对象已被销毁,即调用了 destroy()。
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:
-
DeveloperError : 此对象已被销毁,即调用了 destroy()。
Example:
labels.add(...);
labels.add(...);
labels.removeAll();
