Example labels
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.
参数名称 | 类型 | 描述信息 | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
可选
Object with the following properties:
|
使用示例:
// 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:
参考:
成员(属性)
blendOption : BlendOption
-
默认值:
BlendOption.OPAQUE_AND_TRANSLUCENT
Draws the bounding sphere for each draw command in the primitive.
-
默认值:
false
LabelCollection#get
to iterate over all the labels
in the collection.
modelMatrix : Matrix4
Transforms.eastNorthUpToFixedFrame
.
-
默认值:
Matrix4.IDENTITY
使用示例:
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'
});
-
默认值:
true
方法
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
.
参数名称 | 类型 | 描述信息 |
---|---|---|
options |
Label.ConstructorOptions | 可选 A template describing the label's properties as shown in Example 1. |
返回值:
Throws:
-
DeveloperError : This object was destroyed, i.e., destroy() was called.
使用示例s:
// 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',
});
参考:
参数名称 | 类型 | 描述信息 |
---|---|---|
label |
Label | The label to check for. |
返回值:
Once an object is destroyed, it should not be used; calling any function other than
isDestroyed
will result in a DeveloperError
exception. Therefore,
assign the return value (undefined
) to the object as done in the example.
Throws:
-
DeveloperError : This object was destroyed, i.e., destroy() was called.
使用示例:
labels = labels && labels.destroy();
参考:
get(index) → Label
LabelCollection#length
to iterate over all the labels
in the collection.
Performance:
Expected constant time. If labels were removed from the collection and
Scene#render
was not called, an implicit O(n)
operation is performed.
参数名称 | 类型 | 描述信息 |
---|---|---|
index |
number | The zero-based index of the billboard. |
返回值:
Throws:
-
DeveloperError : This object was destroyed, i.e., destroy() was called.
使用示例:
// 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;
}
参考:
If this object was destroyed, it should not be used; calling any function other than
isDestroyed
will result in a DeveloperError
exception.
返回值:
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.
参数名称 | 类型 | 描述信息 |
---|---|---|
label |
Label | The label to remove. |
返回值:
true
if the label was removed; false
if the label was not found in the collection.
Throws:
-
DeveloperError : This object was destroyed, i.e., destroy() was called.
使用示例:
const l = labels.add(...);
labels.remove(l); // Returns true
参考:
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 : This object was destroyed, i.e., destroy() was called.
使用示例:
labels.add(...);
labels.add(...);
labels.removeAll();