Cesium|xt3d分层单体化
- 效果
- 代码
- 预览地址
效果

代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>cesium|xt3d</title>
<!-- 引入Cesium -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/Build/Cesium/Cesium.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/Build/Cesium/Widgets/widgets.css">
<script src='http://www.xt3d.cn/libs/turf.min.js'></script>
<!-- 使用vue绑定控件 -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/lib/theme-chalk/index.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.js"></script>
<!-- 引入xt3d -->
<script src="http://www.xt3d.cn/xt3dlib/xt3d.min.js"></script>
<style>
html,
body,
#map3d {
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
#infoPanel {
position: absolute;
right: 10px;
top: 90px;
border-radius: 4px;
border: 1px solid rgba(128, 128, 128, 0.5);
color: #ffffff;
background: rgba(0, 0, 0, 0.4);
box-shadow: 0 3px 14px rgb(128 128 128 / 50%);
max-width: 380px;
}
.floor-info-panel {
position: absolute;
right: 20px;
top: 100px;
background: rgba(26, 56, 143, 0.58);
}
.info-header {
background: rgba(26, 56, 143, 0.88);
color: white;
padding: 5px;
}
.info-body {
min-width: 260px;
color: white;
}
.info-item {
margin: 2px 4px;
background: rgba(0, 0, 0, 0.137);
height: 40px;
line-height: 40px;
}
.info-label {
display: inline-block;
min-width: 60px;
text-align: justify;
-moz-text-align-last: justify;
text-align-last: justify;
width: 80px;
}
.form-container {
position: absolute;
left: 10px;
top: 90px;
padding: 10px 15px;
border-radius: 4px;
border: 1px solid rgba(128, 128, 128, 0.5);
color: #ffffff;
background: rgba(0, 0, 0, 0.4);
box-shadow: 0 3px 14px rgb(128 128 128 / 50%);
max-width: 300px;
}
.tip-item {
margin: 2px 0px;
padding: 5px 1px;
}
</style>
</head>
<body>
<div id="map3d"></div>
<div class="form-container">
<div class="tip-item">鼠标左键点击楼栋,获取分层数据。</div>
<div class="tip-item">获取分层数据后,鼠标点击每一层会高亮显示该层并展示该层相关信息</div>
</div>
<div class="floor-info-panel" id="infoPanel" v-if="feature" :feature="feature">
<div class="info-header">楼层信息</div>
<div class="info-body">
<div class="info-item">
<span class="info-label">街道名称:</span>
<span class="info-text">{{feature.properties.StreetName}}</span>
</div>
<div class="info-item">
<span class="info-label">小区名称:</span>
<span class="info-text">{{feature.properties.CommunityN}}</span>
</div>
<div class="info-item">
<span class="info-label">楼栋名称:</span>
<span class="info-text">{{feature.properties.BuildName}}</span>
</div>
<div class="info-item">
<span class="info-label">楼层号:</span>
<span class="info-text">{{feature.properties.FloorNumbe}}</span>
</div>
<div class="info-item">
<span class="info-label">单元数:</span>
<span class="info-text">{{feature.properties.UnitCount}}</span>
</div>
<div class="info-item">
<span class="info-label">底部高度</span>
<span class="info-text">{{feature.properties.BaseHeight}}</span>
</div>
<div class="info-item">
<span class="info-label">楼层高度</span>
<span class="info-text">{{feature.properties.FloorHeigh}}</span>
</div>
<div class="info-item">
<span class="info-label">顶部高度</span>
<span class="info-text">{{feature.properties.TopHeight}}</span>
</div>
</div>
</div>
<script>
let xt3dInit = {
init(el) {
this.initViewer(el);
this.load3dtiles();
this.setView();
this.initDTH();
this.FloorSelectedEvent = new Cesium.Event();
},
//初始化viewer
initViewer(el) {
this.viewer = new Cesium.Viewer(el, {
infoBox: false,
selectionIndicator: false,
navigation: false,
animation: false,
shouldAnimate: false,
timeline: false,
baseLayerPicker: false,
geocoder: false,
homeButton: false,
sceneModePicker: false,
navigationHelpButton: false,
imageryProvider: new Cesium.UrlTemplateImageryProvider({
url: "https://t7.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=tdtTk"
})
});
},
initDTH() {
this.fcDTH = new xt3d.DTH.FC(this.viewer, {
fcDataServerBaseUrl: "http://106.53.103.200:8080/geoserver/xt3d/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=xt3d%3Afc_offset&maxFeatures=50&outputFormat=application%2Fjson"
});
this.fcDTH.FloorSelectedEvent.addEventListener((feature) => {
this.FloorSelectedEvent.raiseEvent(feature); //触发选中事件 通知界面更新
})
this.fcDTH.activate();
},
//加载三维模型
load3dtiles() {
let tileset = this.viewer.scene.primitives.add(
new Cesium.Cesium3DTileset({
url: "http://www.xt3d.cn/data/offset_3dtiles/tileset.json",
})
);
tileset.readyPromise
.then(tileset => {
xt3d.TilesetPlugin.setTilesetHeight(tileset, 58);
})
.otherwise(function(error) {
console.log(error);
});
},
//设置视图
setView() {
let flyToOpts = {
destination: {
x: -1573995.6129586431,
y: 5327777.486713325,
z: 3123087.61152451
},
orientation: {
heading: 4.996705925641388,
pitch: -0.5519860541846482,
roll: 6.279943661313574
}
};
this.viewer.scene.camera.setView(flyToOpts);
},
destroy() {
this.viewer.entities.removeAll();
this.viewer.imageryLayers.removeAll(true);
this.viewer.destroy();
}
}
xt3dInit.init("map3d");
//界面控制
let vm = new Vue({
el: "#infoPanel",
data() {
return {
feature: undefined //分层数据
}
},
})
xt3dInit.FloorSelectedEvent.addEventListener(e => {
vm.feature = e;
})
</script>
</body>
</html>
预览地址
xt3d 在线预览地址