cesium編程入門(九)實體 Entity

cesium編程入門(九)實體 Entity

cesium編程入門(五)繪製形狀提到過添加實體的方法,這一節聊一聊實體相關的一些內容:html

先來看 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

文字標註,能夠設置樣式,文字內容,字體,偏移等等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)
}

模型 model

常見的模型有glTF 和glbsvg

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
}

建立一個實體參考: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

  • pickEntity 獲取最頂層的實體
  • drillPickEntities 獲取當前位置的全部實體的集合(List)
相關文章
相關標籤/搜索