在cesium編程入門(五)繪製形狀提到過添加實體的方法,這一節聊一聊實體相關的一些內容:html
先來看 Entity 的各個屬性編程
文字標註,能夠設置樣式,文字內容,字體,偏移等等api
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) }
常見的模型有glTF 和glbsvg
model : { uri : '../../SampleData/models/CesiumGround/Cesium_Ground.gltf' }
一個最簡單的廣告牌通常就是圖片,和顯示大小字體
billboard : { image : 'http://localhost:81/images/2015/02-02/Philadelphia_Phillies.png', width : 64, height : 64 }
建立一個實體參考:spa
//方法一 var entity = new Entity({ id : 'uniqueId' }); viewer.entities.add(entity); //方法一 簡寫 viewer.entities.add({ id : 'uniqueId' }); //方法二 var entity = viewer.entities.getOrCreateEntity('uniqueId');
查找實體的方法:code
var entity = viewer.entities.getById('uniqueId');
移除實體的方法:htm
//方法一,先查後刪 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)blog