var viewer = new Cesium.Viewer('cesiumContainer');
var dataSource;
viewer.dataSources.removeAll();
//Seed the random number generator for repeatable results.
Cesium.Math.setRandomNumberSeed(0);
dataSource = new Cesium.GeoJsonDataSource();
var promise = dataSource.load('../../SampleData/ne_10m_us_states.topojson');
promise.then(function(dataSource) {
viewer.dataSources.add(dataSource);
//Get the array of entities
var entities = dataSource.entities.values;
var colorHash = {};
for (var i = 0; i < entities.length; i++) {
//For each entity, create a random color based on the state name.
//Some states have multiple entities, so we store the color in a
//hash so that we use the same color for the entire state.
var entity = entities[i];
var name = entity.name;
var color = colorHash[name];
if (!color) {
color = Cesium.Color.fromRandom({
alpha : 1.0
});
colorHash[name] = color;
}
//Set the polygon material to our random color.
entity.polygon.material = color;
//Remove the outlines.
//entity.polygon.outline = false;
//Extrude the polygon based on the state's population. Each entity
//stores the properties for the GeoJSON feature it was created from
//Since the population is a huge number, we divide by 50.
entity.polygon.extrudedHeight = entity.properties.Population / 50.0;
}
}).otherwise(function(error){
//Display any errrors encountered while loading.
window.alert(error);
});
Sandcastle.addToolbarButton('Toggle volume', function() {
//Get the array of entities
var entities = dataSource.entities.values;
dataSource.entities.suspendEvents();
for (var i = 0; i < entities.length; i++) {
var entity = entities[i];
//entity.polygon.height = 0;
if (entity.polygon.extrudedHeight === undefined) {
entity.polygon.extrudedHeight = entity.properties.Population / 50.0;
} else {
entity.polygon.extrudedHeight = undefined;
}
}
dataSource.entities.resumeEvents();
//dataSource.changedEvent.raiseEvent();
//dataSource.entities.collectionChanged.raiseEvent(dataSource.entities, null, null, dataSource.entities.values);
});
参考
https://github.com/CesiumGS/cesium/issues/5590
var polygon = viewer.entities.add({
polygon: {
hierarchy: {
positions: [new Cesium.Cartesian3(6378136.282064899, 2045.931221366772, -1942.1627961569236),
new Cesium.Cartesian3(6378136.401006819, 2059.9615452982202, -2009.4381889716935),
new Cesium.Cartesian3(6378136.323519128, 2120.403271677589, -2010.7963412229758),
new Cesium.Cartesian3(6378136.231778225, 2123.366119812282, -1930.1244599529364)
]
},
perPositionHeight: true,//对每个位置使用options.positions的height,而不使用options.height来确定高度
extrudedHeight: 100,
material: Cesium.Color.BLUE.withAlpha(0.5),
outline: true,
outlineColor: Cesium.Color.BLACK.withAlpha(1)//黑色轮廓线
}
});
viewer.zoomTo(polygon);
var pointArray = new Array();
for (var j = 0; j < data.geometry.points.length; j++) {
var x = data.geometry.points[j].x;
var y = data.geometry.points[j].y;
pointArray.push(x);
pointArray.push(y);
pointArray.push(JMGC + GD);
}
viewer.entities.add({
name:"房间号:" + FJH,
fjhInfo:FJH,
colorInfo:colorStr,
id: FW_HS_INDEX,
text:BDCDYH,
polygon: {
hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights(pointArray),
extrudedHeight: JMGC,//多边形的挤出面和椭圆面之间的距离(以米为单位)。
perPositionHeight: true,//对每个位置使用options.positions的height,而不使用options.height来确定高度
// material : Cesium.Color.RED.withAlpha(0.5),//颜色及透明度设置
material: Cesium.Color.fromAlpha(Cesium.Color.fromCssColorString(colorStr), .5),
outline: true,
outlineColor: Cesium.Color.BLACK.withAlpha(1)//黑色轮廓线
}
});