-
中文小网站:http://cesium.xin/wordpress/
英文网站:https://cesium.com/
先把cesium.js以及css引入,后面的代码就不在显示这些脚本引入
先画简单的点(point)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <!-- Include the CesiumJS JavaScript and CSS files --> <script src="https://cesium.com/downloads/cesiumjs/releases/1.85/Build/Cesium/Cesium.js"></script> <link href="https://cesium.com/downloads/cesiumjs/releases/1.85/Build/Cesium/Widgets/widgets.css" rel="stylesheet"> <style> </style> </head> <body> <div id="cesiumContainer"></div> <script> // Your access token can be found at: https://cesium.com/ion/tokens. // This is the default access token from your ion account Cesium.Ion.defaultAccessToken = 'xxxtoken'; var viewer = new Cesium.Viewer('cesiumContainer');
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
point: {
pixelSize: 10,
color: Cesium.Color.YELLOW,
},
});
</script> </div> </body> </html>
加标签label
var entity = viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-103.0, 40.0), 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) } });
加广告牌(加图片)billboard
var entity = viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-103.0, 40.0), billboard : { image : './images/1.jpg', width : 64, height : 64 } });
划线(ployline)
var redLine = viewer.entities.add({ name: "Red line on terrain", polyline: { id:1, positions: Cesium.Cartesian3.fromDegreesArray([-75, 35, -125, 35]), width: 5, material: new Cesium.PolylineOutlineMaterialProperty({ color: Cesium.Color.fromCssColorString('red').withAlpha('0.5'), outlineWidth:2, outlineColor: Cesium.Color.fromCssColorString('yellow') }), clampToGround: true }, });
画立方体(box)
var redBox = viewer.entities.add({ name : 'Red box with black outline', position: Cesium.Cartesian3.fromDegrees(-107.0, 40.0, 300000.0), box : { dimensions : new Cesium.Cartesian3(400000.0, 300000.0, 500000.0), material : Cesium.Color.RED.withAlpha(0.5), outline : true, outlineColor : Cesium.Color.BLACK } });
czml画立方体(box)
var czml = [{ "id" : "document", "name" : "box", "version" : "1.0" },{ "id" : "shape2", "name" : "Red box with black outline", "position" : { "cartographicDegrees" : [-107.0, 40.0, 300000.0] }, "box" : { "dimensions" : { "cartesian": [400000.0, 300000.0, 500000.0] }, "material" : { "solidColor" : { "color" : { "rgba" : [255, 0, 0, 128] } } }, "outline" : true, "outlineColor" : { "rgba" : [0, 0, 0, 255] } } }]; var dataSourcePromise = Cesium.CzmlDataSource.load(czml); viewer.dataSources.add(dataSourcePromise); viewer.zoomTo(dataSourcePromise);
画椭圆(ellipse) 画好之后 通过实例更改颜色
//方法一,构造时赋材质 var entity = viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-103.0, 40.0), ellipse : { semiMinorAxis : 250000.0, semiMajorAxis : 400000.0, material : Cesium.Color.BLUE.withAlpha(0.5)//可设置不同的MaterialProperty } }); //方法二,构造后赋材质 var ellipse = entity.ellipse; ellipse.material = Cesium.Color.RED;
设置材质(通过entity实例设置背景颜色、背景图、棋盘颜色、条纹纹理、网格、线条发光、线条边框)
地址:http://cesium.xin/wordpress/archives/108.html
形状大全
实体entity的一些方法
增加:
//方法一 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);
增加表述信息:
var entity = viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-103.0, 40.0), billboard : { image : './images/1.jpg', width : 64, height : 64 }, description:'divID'//方法一 }); //方法二 entity.description = '\ <img\ width="50%"\ style="float:left; margin: 0 1em 1em 0;"\ src="./images/1.jpg"/>\ <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>';
-