import mapData from './mapData'; const mapLayer = { layerList: [], locationEntity: null, setLayerChange(res) { if (!res) return; if (res.checked) { this.setAddGeoJsonData(res); } else { this.setRemoveGeoJsonData(res); } }, getlocationEntity(item) { if (!item) return; if (this.locationEntity) { this.setRemovelocationEntity(); } const source = Viewer.dataSources._dataSources; var sources_data = source.filter(res => { if (res._name == item.attributes.XL) { return res; } }); if (sources_data.length <= 0) return; const entities = sources_data[0].entities.values; for (var i in entities) { if (entities[i].properties.FID._value == item.attributes.FID) { if (item.attributes.MC) { entities[i].label.text._value = item.attributes.MC; } else if (item.attributes.SWLYMC) { entities[i].label.text._value = item.attributes.SWLYMC; } else if (item.attributes.XMMC) { entities[i].label.text._value = item.attributes.XMMC; } else if (item.attributes.TDQLR) { entities[i].label.text._value = item.attributes.TDQLR; } this.locationEntity = entities[i]; if (this.locationEntity) { this.setTimeIconChange(this.locationEntity); } } } }, setTimeIconChange(res) { const start = Cesium.JulianDate.fromDate(new Date()); const stop = Cesium.JulianDate.addSeconds(start, 10, new Cesium.JulianDate()); Viewer.clock.startTime = start.clone(); Viewer.clock.stopTime = stop.clone(); Viewer.clock.multiplier = 1; Viewer.clock.clockRange = Cesium.ClockRange.CLAMPED; const that = this; Viewer.clock.onTick.addEventListener(adjustRate); Viewer.clock.shouldAnimate = true; function adjustRate(clock) { //可以在这里做一些时间的监听 if (that.locationEntity.billboard.scale) { if (that.locationEntity.billboard.scale < 1.5) { that.locationEntity.billboard.scale = that.locationEntity.billboard.scale + 0.02; } else { Viewer.clock.shouldAnimate = false; Viewer.clock.canAnimate = false; Viewer.clock.currentTime = Viewer.clock.startTime; Viewer.clock.onTick.removeEventListener(adjustRate); } } else { that.locationEntity.billboard.scale = 1; } } }, setRemovelocationEntity() { if (this.locationEntity) { this.locationEntity.billboard.scale = 1; this.locationEntity.label.text._value = ''; this.locationEntity = null; } }, setAddGeoJsonData(res) { const obj = this.getFeatureEntity(res); var geoJson = this.getGeoJsonData(obj); if (geoJson.features.length <= 0) return; Cesium.GeoJsonDataSource.load(geoJson).then(dataSource => { dataSource.name = res.name; Viewer.dataSources.add(dataSource); const entities = dataSource.entities.values; for (var i = 0; i < entities.length; i++) { const entity = entities[i]; entity.billboard = { image: res.icon, width: 32, height: 32, pixelOffset: new Cesium.Cartesian3(0, 0, -10), borderWidth: 20, borderColor: Cesium.Color.fromCssColorString(' rgba(22, 119, 255, 1)'), clampToGround: true, distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, config.distance), disableDepthTestDistance: Number.POSITIVE_INFINITY }; entity.label = { text: '', font: "15px 'Microsoft Yahei'", fillColor: Cesium.Color.fromCssColorString(' rgba(22, 119, 255, 1)'), // 文本填充颜色 rgba(22, 119, 255, 1) pixelOffset: new Cesium.Cartesian3(0, -45, 0), // fillColor: Cesium.Color.BLIUE, showBackground: true, //显示背景 backgroundColor: Cesium.Color.fromCssColorString('rgba(255, 255, 255, 8)'), //背景色 backgroundPadding: new Cesium.Cartesian2(5, 6), //padding值 outlineColor: Cesium.Color.fromCssColorString('rgba(169, 169, 169, 1)'), // 文本轮廓颜色169, 169, 169 distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, config.distance) }; } }); }, setRemoveGeoJsonData(res) { var source = Viewer.dataSources._dataSources; Viewer.dataSources.remove(Viewer.dataSources.getByName(res.name)[0]); }, getGeoJsonData(res) { return { type: 'FeatureCollection', totalFeatures: 0, features: res }; }, getFeatureEntity(res) { var std = []; mapData.searchData.filter(item => { if (item.attributes.XL === res.name) { const coord = item.geometry.coordinates; std.push({ properties: item.attributes, type: 'Feature', geometry: { type: 'Point', coordinates: [coord[0], coord[1]] } }); } // if (item.pid == res.id) { // return item; // } }); return std; }, setLayerRemoveAll() { Viewer.dataSources.removeAll(); } }; export default mapLayer;