1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
| <template> <div class="main"> <div id="cesiumContainer"></div> <div class="btn"> <van-button :type="type == 1 ? 'primary' : 'default'" @click="changeType(1, $event)" style="float: left">按钮1</van-button> <van-button :type="type == 2 ? 'primary' : 'default'" @click="changeType(2, $event)" style="float: right">按钮2</van-button> </div> </div> </template>
<script> let viewer = null let nodesPosition = null let labels = null let nodesInstances = [] let GInstances = [] let LoadInstances = [] let linesInstances = [] let websocketMap = null import { Button } from 'vant'; export default { name: "", data () { return { type: 1 } }, methods:{ initCesium(){ Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees( 75.0, // 东 0.0, // 南 140.0, // 西 60.0 // 北 ) viewer = new Cesium.Viewer("cesiumContainer", { animation: false, timeline: false, fullscreenButton: false, geocoder: false, baseLayerPicker: false, homeButton: false, sceneModePicker: false, navigationHelpButton: false, selectionIndicator: false, shouldAnimate : true, infoBox: false, // imageryProvider: imageryProvider, baseLayerPicker: false, orderIndependentTranslucency: false, contextOptions: { webgl: { alpha: true, } }, sceneMode: 2 } ) viewer._cesiumWidget._creditContainer.style.display = "none" // 去除版权信息 viewer.imageryLayers.get(0).show = false;//不显示底图 viewer.scene.globe.baseColor = Cesium.Color.fromCssColorString("rgba(65, 175, 227, 1)") viewer.scene.globe.depthTestAgainstTerrain = false viewer.camera.setView({ // Cesium的坐标是以地心为原点,一向指向南美洲,一向指向亚洲,一向指向北极州 // fromDegrees()方法,将经纬度和高程转换为世界坐标 destination:Cesium.Cartesian3.fromDegrees(92.64, 33.93, 3000000.0), orientation:{ // 指向 heading:Cesium.Math.toRadians(180,0), // 视角 pitch:Cesium.Math.toRadians(-90), roll:0.0 } }) }, getPosition(){ this.axios.get(`http://xxx.xxx.xxx.xxx:xxxx/getMap.json`, { }).then((res) => { console.log(res) nodesPosition = res.data this.createNodes() this.createLines() this.addPrimitives() }).catch((err) => { console.log(err) }) }, createNodes(){ nodesPosition.bus.forEach( point => { nodesInstances.push( new Cesium.GeometryInstance( { geometry : new Cesium.CircleGeometry( { center : Cesium.Cartesian3.fromDegrees( point.lng, point.lat, 1000 ), radius : 30000.0, vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT }), id: point.name, attributes : { color : Cesium.ColorGeometryInstanceAttribute.fromColor( Cesium.Color.fromHsl(0.58, 1, 0.5, 0.5)) }, })) labels.add({ position: new Cesium.Cartesian3.fromDegrees(point.lng, point.lat, 200000), text: point.name.slice(3), font: '20px Helvetica', horizontalOrigin : Cesium.HorizontalOrigin.CENTER, verticalOrigin : Cesium.VerticalOrigin.CENTER, }) }) nodesPosition.gen.forEach( point => { GInstances.push( new Cesium.GeometryInstance( { geometry : new Cesium.CircleGeometry( { center : Cesium.Cartesian3.fromDegrees( point.lng, point.lat ), radius : 20000.0, }), })) }) nodesPosition.load.forEach( point => { LoadInstances.push( new Cesium.GeometryInstance( { geometry : new Cesium.CircleGeometry( { center : Cesium.Cartesian3.fromDegrees( point.lng, point.lat ), radius : 20000.0, }) })) }) }, createLines(){ let lines = nodesPosition.busLine.concat(nodesPosition.GenLine.concat(nodesPosition.LoadLine)) lines.forEach( (line) => { linesInstances.push( new Cesium.GeometryInstance( { geometry : new Cesium.PolylineGeometry({ positions : Cesium.Cartesian3.fromDegreesArray([ line.Flng, line.Flat, line.Tlng, line.Tlat ]), width: 1 }), id: line.Line_name, attributes : { color : Cesium.ColorGeometryInstanceAttribute.fromColor( Cesium.Color.WHITE) }, })) }) }, addPrimitives(){ viewer.scene.primitives.add( new Cesium.Primitive( { geometryInstances : nodesInstances, //合并 //某些外观允许每个几何图形实例分别指定某个属性,例如: appearance : new Cesium.PerInstanceColorAppearance() })) viewer.scene.primitives.add( new Cesium.Primitive( { geometryInstances : linesInstances, appearance : new Cesium.PolylineColorAppearance({ translucent: false, //是否透明 }) })) viewer.scene.primitives.add( new Cesium.Primitive( { geometryInstances : GInstances, appearance :new Cesium.MaterialAppearance({ material: new Cesium.Material({ fabric: { type: "Image", uniforms: { image: require('@/assets/images/G.png') } } }), materialSupport : Cesium.MaterialAppearance.MaterialSupport.TEXTURED }) })) viewer.scene.primitives.add( new Cesium.Primitive( { geometryInstances : LoadInstances, appearance :new Cesium.MaterialAppearance({ material: new Cesium.Material({ fabric: { type: "Image", uniforms: { image: require('@/assets/images/load.png') } } }), materialSupport : Cesium.MaterialAppearance.MaterialSupport.TEXTURED }) })) }, updateNodes(){ let p = viewer.scene.primitives.get(1) console.log(p) // 连接 websocket websocketMap = new WebSocket('ws://xxx.xxx.xxx.xx:xxxx/Transient') websocketMap.onopen = function (event){ console.log("websocketMap建立链接") websocketMap.send('1') } websocketMap.onmessage = function(event){ let data = JSON.parse(event.data) // console.log(data) for(let i = 0;i < data.bus.length; i++){ let attributes = p.getGeometryInstanceAttributes(`bus${data.bus[i].id}`) attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.fromHsl(data.bus[i].value*0.25, 1, 0.5, 0.8)) } } }, changeType(type, event){ console.log('发送数据', type, event.target) let b = event.target this.type = type websocketMap.send(type) } }, created(){ }, mounted() { this.initCesium() labels = viewer.scene.primitives.add(new Cesium.LabelCollection()) this.getPosition() setTimeout(() => { this.updateNodes() }, 1000) } } </script> <style scoped> .main{ width: 100%; height: 100%; } #cesiumContainer{ width: 100%; height: 100%; } .btn{ width: 400px; height: 80px; position: absolute; top: 100px; left: 50%; margin-left: -200px } </style>
|