注意:我在使用Cesium会出现跨域问题,所以我使用nginx做的代理
1、准备工作
先去cesium ion申请token
2、cesium加载geojson数据
geojson数据可以通过此地址自己绘制生成geojson数据https://geojson.io
<div id="cesiumContainer"></div>
var cesiumAsset = '你的token';
Cesium.Ion.defaultAccessToken = cesiumAsset;
const viewer = new Cesium.Viewer('cesiumContainer', {
homeButton: false, //home按钮
sceneModePicker: false, //模式切换按钮
baseLayerPicker: false, //右上角的图层选择按钮
navigationHelpButton: false, //右上角的帮助按钮
animation: false, //左下角的动画仪表盘
timeline: false, //底部的时间轴
fullscreenButton: false, //右下角的全屏按钮
vrButton: false, //vr按钮
infoBox: false, //点击实体的信息框
selectionIndicator: false, //点击实体的绿色小框
geocoder: false, //搜索框
});
viewer._cesiumWidget._creditContainer.style.display = "none"; //隐藏cesium ion
这样,你的地球就加载出来了
然后添加geojson数据到地球上
Cesium.GeoJsonDataSource.load('http://localhost:3000/json/test1.json', {
stroke: Cesium.Color.fromCssColorString('rgba(0, 0, 0, 1)'),
fill: Cesium.Color.PINK.withAlpha(0.5),
strokeWidth: 3
});
3 将加载的geojson数据立体拉伸
我这里是通过设置extrudedHeight设置实体的高度,在通过height设置离地面的高度来实现一层一层的效果,当然也可以只设置extrudedHeight实现拉伸效果
Cesium.GeoJsonDataSource.load('http://localhost:3000/json/test1.json', {
stroke: Cesium.Color.fromCssColorString('rgba(0, 0, 0, 1)'),
//fill: Cesium.Color.PINK.withAlpha(0.5),
strokeWidth: 3
}).then(function (dataSource) {
viewer.dataSources.add(dataSource);
var entities = dataSource.entities.values;
var zrzHeight;
var zrzLc;
for (let i = 0; i < entities.length; i++) {
let entity = entities[i];
let name = entity.name;
entity.polygon.outline = true;
if (entity._properties._isDS._value == 1) {
for (var j = 0; j < entity._properties._lc._value; j++) {
var entityes = entity;
entityes._id = entity._properties._name._value + (j + 1);
var en = new Cesium.Entity(entityes);
en.polygon.material = Cesium.Color.fromCssColorString('rgba(255,255,255,1)');
en.polygon.extrudedHeight = (j + 1) * 5;
en.polygon.height = (j * 5);
viewer.entities.add(en);
}
}
else if (entity._properties._isDS._value == -1) {
for (var j = 0; j < entity._properties._lc._value; j++) {
var entityes = entity;
entityes._id = entity._properties._name._value + (j + 1);
var en = new Cesium.Entity(entityes);
en.polygon.material = Cesium.Color.fromCssColorString('rgba(255,255,255,1)');
en.polygon.extrudedHeight = - (j + 1) * 5;
en.polygon.height = - (j * 5);
viewer.entities.add(en);
}
}
}
});
4、设置地球能看到地下
viewer.scene.screenSpaceCameraController.enableCollisionDetection = false; //设置鼠标进入地下
//设置地球透明度
viewer.scene.globe.translucency.enabled = true;
viewer.scene.globe.translucency.frontFaceAlphaByDistance = new Cesium.NearFarScalar(400.0, 0.5, 2000, 0.9);
5、定位到指定位置
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(30.015676845995472, 8.13054316835077, 1000),
orientation: {
heading: Cesium.Math.toRadians(348.4202942851978),
pitch: Cesium.Math.toRadians(-89.74026687972041),
roll: Cesium.Math.toRadians(0)
},
complete: function callback() {
// 定位完成之后的回调函数
}
});
6、完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cesium加载GeoJson数据</title>
<script src="http://localhost:3000/Cesium/Cesium.js"></script>
<link rel="stylesheet" href="http://localhost:3000/Widgets/widgets.css" />
<script src="http://localhost:3000/js/jquery.js"></script>
<style type="text/css">
html,
body,
#cesiumContainer{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="cesiumContainer"></div>
<script>
var cesiumAsset = '你的token';
Cesium.Ion.defaultAccessToken = cesiumAsset;
const viewer = new Cesium.Viewer('cesiumContainer', {
homeButton: false, //home按钮
sceneModePicker: false, //模式切换按钮
baseLayerPicker: false, //右上角的图层选择按钮
navigationHelpButton: false, //右上角的帮助按钮
animation: false, //左下角的动画仪表盘
timeline: false, //底部的时间轴
fullscreenButton: false, //右下角的全屏按钮
vrButton: false, //vr按钮
infoBox: false, //点击实体的信息框
selectionIndicator: false, //点击实体的绿色小框
geocoder: false, //搜索框
});
viewer._cesiumWidget._creditContainer.style.display = "none"; //隐藏cesium ion
viewer.scene.screenSpaceCameraController.enableCollisionDetection = false; //设置鼠标进入地下
//设置地球透明度
viewer.scene.globe.translucency.enabled = true;
viewer.scene.globe.translucency.frontFaceAlphaByDistance = new Cesium.NearFarScalar(400.0, 0.5, 2000, 0.9);
// 将三维球定位到指定位置
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(30.015676845995472, 8.13054316835077, 1000),
orientation: {
heading: Cesium.Math.toRadians(348.4202942851978),
pitch: Cesium.Math.toRadians(-89.74026687972041),
roll: Cesium.Math.toRadians(0)
},
complete: function callback() {
// 定位完成之后的回调函数
}
});
//加载GeoJson数据
Cesium.GeoJsonDataSource.load('http://localhost:3000/json/test1.json', {
stroke: Cesium.Color.fromCssColorString('rgba(0, 0, 0, 1)'),
//fill: Cesium.Color.PINK.withAlpha(0.5),
strokeWidth: 3
}).then(function (dataSource) {
viewer.dataSources.add(dataSource);
var entities = dataSource.entities.values;
var zrzHeight;
var zrzLc;
for (let i = 0; i < entities.length; i++) {
let entity = entities[i];
let name = entity.name;
entity.polygon.outline = true;
if (entity._properties._isDS._value == 1) {
for (var j = 0; j < entity._properties._lc._value; j++) {
var entityes = entity;
entityes._id = entity._properties._name._value + (j + 1);
var en = new Cesium.Entity(entityes);
en.polygon.material = Cesium.Color.fromCssColorString('rgba(255,255,255,1)');
en.polygon.extrudedHeight = (j + 1) * 5;
en.polygon.height = (j * 5);
viewer.entities.add(en);
}
}
else if (entity._properties._isDS._value == -1) {
for (var j = 0; j < entity._properties._lc._value; j++) {
var entityes = entity;
entityes._id = entity._properties._name._value + (j + 1);
var en = new Cesium.Entity(entityes);
en.polygon.material = Cesium.Color.fromCssColorString('rgba(255,255,255,1)');
en.polygon.extrudedHeight = - (j + 1) * 5;
en.polygon.height = - (j * 5);
viewer.entities.add(en);
}
}
}
});
</script>
</body>
</html>