cesium編程入門(九)實體 Entity
在cesium編程入門(五)繪制形狀提到過添加實體的方法,這一節聊一聊實體相關的一些內容:
先來看 Entity 的各個屬性
- id
唯一標志,如果沒設置,值就為一個默認給定的GUID - name
名稱,可以不唯一 - availability
可用性 - show
可見性 - description
描述 - position
位置 - orientation
方向 - viewFrom
查看此對象的初始偏移量 - parent
父節點 - properties
與此實體關聯的任意屬性。 - Graphics
相關的形狀- box
- corridor
- cylinder
- ellipse
- ellipsoid
- path
- point
- polygon
- polyline
- polylineVolume
- rectangle
- wall
- billboard
- label
- model
第五篇中描述了部分形狀,這里補充 標簽,模型,廣告牌
標簽 label
文字標注,可以設置樣式,文字內容,字體,偏移等等
label : {
text : 'Citizens Bank Park',
font : '14pt monospace',
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
outlineWidth : 2,
verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
pixelOffset : new Cesium.Cartesian2(0, -9)
}
模型 model
常見的模型有glTF 和glb
model : {
uri : '../../SampleData/models/CesiumGround/Cesium_Ground.gltf'
}
廣告牌 billboard
一個最簡單的廣告牌一般就是圖片,和顯示大小
billboard : {
image : 'http://localhost:81/images/2015/02-02/Philadelphia_Phillies.png',
width : 64,
height : 64
}
增
創建一個實體參考:
//方法一
var entity = new Entity({
id : 'uniqueId'
});
viewer.entities.add(entity);
//方法一 簡寫
viewer.entities.add({
id : 'uniqueId'
});
//方法二
var entity = viewer.entities.getOrCreateEntity('uniqueId');
查
查找實體的方法:
var entity = viewer.entities.getById('uniqueId');
刪
移除實體的方法:
//方法一,先查后刪
var entity = viewer.entities.getById('uniqueId');
viewer.entities.remove(entity)
//方法二,直接刪除
viewer.entities.removeById('uniqueId')
//方法三,刪除所有
viewer.entities.removeAll()
實體集變化
function onChanged(collection, added, removed, changed){
var msg = 'Added ids';
for(var i = 0; i < added.length; i++) {
msg += '\n' + added[i].id;
}
console.log(msg);
}
viewer.entities.collectionChanged.addEventListener(onChanged);
描述信息 官網示例
只需要修改entity 的description 屬性就可以達到目的
var viewer = new Cesium.Viewer('cesiumContainer');
var wyoming = viewer.entities.add({
name : 'Wyoming',
polygon : {
hierarchy : Cesium.Cartesian3.fromDegreesArray([
-109.080842,45.002073,
-105.91517,45.002073,
-104.058488,44.996596,
-104.053011,43.002989,
-104.053011,41.003906,
-105.728954,40.998429,
-107.919731,41.003906,
-109.04798,40.998429,
-111.047063,40.998429,
-111.047063,42.000709,
-111.047063,44.476286,
-111.05254,45.002073]),
height : 0,
material : Cesium.Color.RED.withAlpha(0.5),
outline : true,
outlineColor : Cesium.Color.BLACK
},
description:'divID'//方法一
});
viewer.zoomTo(wyoming);
//方法二
wyoming.description = '\
<img\
width="50%"\
style="float:left; margin: 0 1em 1em 0;"\
src="//cesiumjs.org/images/2015/02-02/Flag_of_Wyoming.svg"/>\
<p>\
Wyoming is a state in the mountain region of the Western \
United States.\
</p>\
<p>\
Wyoming is the 10th most extensive, but the least populous \
and the second least densely populated of the 50 United \
States. The western two thirds of the state is covered mostly \
with the mountain ranges and rangelands in the foothills of \
the eastern Rocky Mountains, while the eastern third of the \
state is high elevation prairie known as the High Plains. \
Cheyenne is the capital and the most populous city in Wyoming, \
with a population estimate of 62,448 in 2013.\
</p>\
<p>\
Source: \
<a style="color: WHITE"\
target="_blank"\
href="http://en.wikipedia.org/wiki/Wyoming">Wikpedia</a>\
</p>';
選中
scene 提供了兩個方法來獲取選中對象,參數都是兩個(viewer, windowPosition)
- pickEntity 獲取最頂層的實體
- drillPickEntities 獲取當前位置的所有實體的集合(List)