Label

internal constructor new Cesium.Label(options, labelCollection)

通过调用 LabelCollection#add 创建标签。不要直接调用构造函数。
Name Type Description
options Label.ConstructorOptions 描述初始化选项的对象
labelCollection LabelCollection LabelCollection 实例
Throws:
  • DeveloperError : translucencyByDistance.far 必须大于 translucencyByDistance.near
  • DeveloperError : pixelOffsetScaleByDistance.far 必须大于 pixelOffsetScaleByDistance.near
  • DeveloperError : distanceDisplayCondition.far 必须大于 distanceDisplayCondition.near
Demo:
See:

Members

static Cesium.Label.enableRightToLeftDetection : boolean

确定是否运行将标签文本匹配到从右到左语言的算法。
Default Value: false
Examples:
// 示例 1。
// 在初始化前设置标签的 rightToLeft
Cesium.Label.enableRightToLeftDetection = true;
const myLabelEntity = viewer.entities.add({
  label: {
    id: 'my label',
    text: 'זה טקסט בעברית \n ועכשיו יורדים שורה',
  }
});
// 示例 2。
const myLabelEntity = viewer.entities.add({
  label: {
    id: 'my label',
    text: 'English text'
  }
});
// 在初始化后设置标签的 rightToLeft
Cesium.Label.enableRightToLeftDetection = true;
myLabelEntity.text = 'טקסט חדש';
Gets or sets the background color of this label.
Default Value: new Color(0.165, 0.165, 0.165, 0.8)
Gets or sets the background padding, in pixels, of this label. The x value controls horizontal padding, and the y value controls vertical padding.
Default Value: new Cartesian2(7, 5)

disableDepthTestDistance : number|undefined

获取或设置从相机到此距离之外,深度测试将被禁用——例如,防止与地形裁剪。 当设置为 undefined0 时,始终应用深度测试。当设置为 Number.POSITIVE_INFINITY 时,从不应用深度测试。
Default Value: undefined
获取或设置指定在距离相机多远距离显示此标签的条件。
Default Value: undefined
获取或设置在眼坐标中应用于此标签的 3D 笛卡尔偏移。眼坐标系是左手坐标系,其中 x 指向观察者的右侧,y 指向上方,z 指向屏幕内。眼坐标使用与世界坐标和模型坐标相同的比例,通常为米。

眼偏移通常用于在同一位置排列多个标签或对象,例如将标签排列在其对应的 3D 模型上方。

下面,标签位于地球中心,但眼偏移使其始终出现在地球上方,无论观察者或地球的朝向如何。

l.eyeOffset = new Cartesian3(0.0, 8000000.0, 0.0);

Default Value: Cartesian3.ZERO
Gets or sets the fill color of this label.
Default Value: Color.WHITE
See:
Gets or sets the font used to draw this label. Fonts are specified using the same syntax as the CSS 'font' property.
Default Value: '30px sans-serif'
See:
Gets or sets the height reference of this billboard.
Default Value: HeightReference.NONE
获取或设置此标签的水平原点,决定标签绘制在其锚点位置的左侧、中心还是右侧。


Default Value: HorizontalOrigin.LEFT
Example:
// 使用顶部、右侧原点
l.horizontalOrigin = Cesium.HorizontalOrigin.RIGHT;
l.verticalOrigin = Cesium.VerticalOrigin.TOP;
获取或设置拾取标签时返回的用户定义值。
Gets or sets the outline color of this label.
Default Value: Color.BLACK
See:

outlineWidth : number

Gets or sets the outline width of this label.
Default Value: 1.0
See:
Gets or sets the pixel offset in screen space from the origin of this label. This is commonly used to align multiple labels and billboards at the same position, e.g., an image and text. The screen space origin is the top, left corner of the canvas; x increases from left to right, and y increases from top to bottom.

default
l.pixeloffset = new Cartesian2(25, 75);
The label's origin is indicated by the yellow point.
Default Value: Cartesian2.ZERO

pixelOffsetScaleByDistance : NearFarScalar

Gets or sets near and far pixel offset scaling properties of a Label based on the Label's distance from the camera. A label's pixel offset will be scaled between the NearFarScalar#nearValue and NearFarScalar#farValue while the camera distance falls within the lower and upper bounds of the specified NearFarScalar#near and NearFarScalar#far. Outside of these ranges the label's pixel offset scaling remains clamped to the nearest bound. If undefined, pixelOffsetScaleByDistance will be disabled.
Examples:
// Example 1.
// Set a label's pixel offset scale to 0.0 when the
// camera is 1500 meters from the label and scale pixel offset to 10.0 pixels
// in the y direction the camera distance approaches 8.0e6 meters.
text.pixelOffset = new Cesium.Cartesian2(0.0, 1.0);
text.pixelOffsetScaleByDistance = new Cesium.NearFarScalar(1.5e2, 0.0, 8.0e6, 10.0);
// Example 2.
// disable pixel offset by distance
text.pixelOffsetScaleByDistance = undefined;
Gets or sets the Cartesian position of this label.
获取或设置与标签像素大小相乘的统一缩放比例。 缩放比例为 1.0 时不改变标签大小;大于 1.0 会放大标签;小于 1.0 的正数会缩小标签。

应用较大的缩放值可能会导致标签像素化。要在不像素化的情况下放大文本,请在调用 Label#font 时使用更大的字体大小。


上图从左到右的缩放比例分别为 0.51.02.0
Default Value: 1.0
Gets or sets near and far scaling properties of a Label based on the label's distance from the camera. A label's scale will interpolate between the NearFarScalar#nearValue and NearFarScalar#farValue while the camera distance falls within the lower and upper bounds of the specified NearFarScalar#near and NearFarScalar#far. Outside of these ranges the label's scale remains clamped to the nearest bound. If undefined, scaleByDistance will be disabled.
Examples:
// Example 1.
// Set a label's scaleByDistance to scale by 1.5 when the
// camera is 1500 meters from the label and disappear as
// the camera distance approaches 8.0e6 meters.
label.scaleByDistance = new Cesium.NearFarScalar(1.5e2, 1.5, 8.0e6, 0.0);
// Example 2.
// disable scaling by distance
label.scaleByDistance = undefined;
确定是否显示此标签。使用此属性隐藏或显示标签,而不是 将其从集合中移除并重新添加。
Default Value: true

showBackground : boolean

Determines if a background behind this label will be shown.
Default Value: false
Gets or sets the style of this label.
Default Value: LabelStyle.FILL
Gets or sets the text of this label.

totalScale : number

获取标签的总缩放比例,即标签的缩放比例乘以所需字体与生成的字形大小相比的计算相对大小。
Default Value: 1.0
Gets or sets near and far translucency properties of a Label based on the Label's distance from the camera. A label's translucency will interpolate between the NearFarScalar#nearValue and NearFarScalar#farValue while the camera distance falls within the lower and upper bounds of the specified NearFarScalar#near and NearFarScalar#far. Outside of these ranges the label's translucency remains clamped to the nearest bound. If undefined, translucencyByDistance will be disabled.
Examples:
// Example 1.
// Set a label's translucencyByDistance to 1.0 when the
// camera is 1500 meters from the label and disappear as
// the camera distance approaches 8.0e6 meters.
text.translucencyByDistance = new Cesium.NearFarScalar(1.5e2, 1.0, 8.0e6, 0.0);
// Example 2.
// disable translucency by distance
text.translucencyByDistance = undefined;
获取或设置此标签的垂直原点,决定标签位于其锚点位置的上方、下方还是中心。


Default Value: VerticalOrigin.BASELINE
Example:
// 使用顶部、右侧原点
l.horizontalOrigin = Cesium.HorizontalOrigin.RIGHT;
l.verticalOrigin = Cesium.VerticalOrigin.TOP;

Methods

computeScreenSpacePosition(scene, result)Cartesian2

计算标签原点的屏幕空间位置,考虑眼偏移和像素偏移。 屏幕空间原点是画布的左上角;x 从左到右增加,y 从上到下增加。
Name Type Description
scene Scene 标签所在的场景。
result Cartesian2 optional 存储结果的对象。
Returns:
标签的屏幕空间位置。
Example:
console.log(l.computeScreenSpacePosition(scene).toString());
See:

equals(other)boolean

确定此标签是否等于另一个标签。如果所有属性 相等,则标签相等。不同集合中的标签也可以相等。
Name Type Description
other Label optional 用于比较相等性的标签。
Returns:
如果标签相等则为 true;否则为 false

isDestroyed()boolean

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

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

Type Definitions

Cesium.Label.ConstructorOptions

Label 构造函数的初始化选项
Properties:
Name Type Attributes Default Description
position Cartesian3 标签的笛卡尔位置。
id * <optional>
使用 Scene#pick 拾取标签时返回的用户定义对象。
show boolean <optional>
true 确定是否显示此标签。
text string <optional>
指定标签文本的字符串。
font string <optional>
'30px sans-serif' 指定用于绘制此标签的字体的字符串。字体使用与 CSS 'font' 属性相同的语法指定。
style LabelStyle <optional>
LabelStyle.FILL 指定标签样式的 LabelStyle
scale number <optional>
1.0 指定与标签大小相乘的统一缩放比例的数值。
showBackground boolean <optional>
false 确定是否显示标签后面的背景。
backgroundColor Color <optional>
new Color(0.165, 0.165, 0.165, 0.8) 指定标签背景颜色的 Color
backgroundPadding Cartesian2 <optional>
new Cartesian2(7, 5) 指定以像素为单位的水平和垂直背景填充的 Cartesian2
pixelOffset Cartesian2 <optional>
Cartesian2.ZERO 指定屏幕空间中从标签原点开始的像素偏移的 Cartesian2
eyeOffset Cartesian3 <optional>
Cartesian3.ZERO 指定在眼坐标中应用于此标签的 3D 笛卡尔偏移的 Cartesian3
horizontalOrigin HorizontalOrigin <optional>
HorizontalOrigin.LEFT 指定此标签水平原点的 HorizontalOrigin
verticalOrigin VerticalOrigin <optional>
VerticalOrigin.BASELINE 指定此标签垂直原点的 VerticalOrigin
heightReference HeightReference <optional>
HeightReference.NONE 指定此标签高度参考的 HeightReference
fillColor Color <optional>
Color.WHITE 指定标签填充颜色的 Color
outlineColor Color <optional>
Color.BLACK 指定标签轮廓颜色的 Color
outlineWidth number <optional>
1.0 指定标签轮廓宽度的数值。
translucencyByDistance NearFarScalar <optional>
基于标签与相机距离指定标签近远半透明属性的 NearFarScalar
pixelOffsetScaleByDistance NearFarScalar <optional>
基于标签与相机距离指定标签近远像素偏移缩放属性的 NearFarScalar
scaleByDistance NearFarScalar <optional>
基于标签与相机距离指定标签近远缩放属性的 NearFarScalar
distanceDisplayCondition DistanceDisplayCondition <optional>
指定在距离相机多远距离显示此标签的 DistanceDisplayCondition
disableDepthTestDistance number <optional>
从相机到此距离之外,深度测试将被禁用——例如,防止与地形裁剪。
Need help? The fastest way to get answers is from the community and team on the Cesium Forum.