init
This commit is contained in:
136
src/Obj/Base/KML/index.js
Normal file
136
src/Obj/Base/KML/index.js
Normal file
@ -0,0 +1,136 @@
|
||||
/**
|
||||
* @name: kml
|
||||
* @author: Administrator
|
||||
* @date: 2023-07-19 09:25
|
||||
* @description:kml
|
||||
* @update: 2023-07-19 09:25
|
||||
*/
|
||||
import Base from '../index'
|
||||
import { setActiveViewer, closeRotateAround, closeViewFollow} from '../../../Global/global'
|
||||
|
||||
class KML extends Base {
|
||||
constructor(sdk, options = {}) {
|
||||
super(sdk, options)
|
||||
this.source = new Cesium.CustomDataSource(this.options.id)
|
||||
// this.source = new Cesium.CompositeEntityCollection([])
|
||||
this.detail = []
|
||||
// this.load()
|
||||
}
|
||||
|
||||
get show() {
|
||||
return this.options.show
|
||||
}
|
||||
|
||||
set show(val) {
|
||||
if (this.source) {
|
||||
this.source.show = val
|
||||
this.options.show = val
|
||||
}
|
||||
}
|
||||
|
||||
setDefaultValue() {
|
||||
this.options.id = this.options.id || Cesium.createGuid()
|
||||
this.options.url = this.options.url || ''
|
||||
this.options.show = this.options.show ?? true
|
||||
}
|
||||
|
||||
async flyTo(duration = 3) {
|
||||
setActiveViewer(0)
|
||||
closeRotateAround(this.sdk)
|
||||
closeViewFollow(this.sdk)
|
||||
|
||||
if (this.options.customView && this.options.customView.relativePosition && this.options.customView.orientation) {
|
||||
let orientation = {
|
||||
heading: Cesium.Math.toRadians(this.options.customView.orientation.heading || 0.0),
|
||||
pitch: Cesium.Math.toRadians(this.options.customView.orientation.pitch || -60.0),
|
||||
roll: Cesium.Math.toRadians(this.options.customView.orientation.roll || 0.0)
|
||||
}
|
||||
|
||||
let lng = this.options.customView.relativePosition.lng
|
||||
let lat = this.options.customView.relativePosition.lat
|
||||
let alt = this.options.customView.relativePosition.alt
|
||||
let destination = Cesium.Cartesian3.fromDegrees(lng, lat, alt)
|
||||
this.sdk.viewer.camera.flyTo({
|
||||
destination: destination,
|
||||
orientation: orientation,
|
||||
duration
|
||||
})
|
||||
}
|
||||
else {
|
||||
if (this.source) this.viewer.flyTo(this.source, { duration })
|
||||
}
|
||||
}
|
||||
|
||||
remove() {
|
||||
super.remove()
|
||||
this.viewer.dataSources.remove(this.source)
|
||||
this.source = null
|
||||
}
|
||||
|
||||
async on() {
|
||||
this.show = this.options.show
|
||||
let source = await Cesium.KmlDataSource.load(this.options.url, {
|
||||
camera: this.viewer.scene.camera,
|
||||
canvas: this.viewer.scene.canvas
|
||||
// clampToGround: true
|
||||
})
|
||||
source.entities.values.forEach((entity, index) => {
|
||||
this.detail.push({ name: entity.name, id: entity.id })
|
||||
if (entity.label) {
|
||||
let scale = 1
|
||||
if (entity.billboard) {
|
||||
scale = entity.billboard.scale._value
|
||||
}
|
||||
entity.label.pixelOffset = new Cesium.Cartesian2(0, -32 * scale - 15)
|
||||
entity.label.horizontalOrigin = Cesium.HorizontalOrigin.CENTER
|
||||
entity.label.disableDepthTestDistance = Number.POSITIVE_INFINITY
|
||||
}
|
||||
if (entity.polygon) {
|
||||
//polygon需要重写,不然无法贴地
|
||||
let polygon = {
|
||||
hierarchy: entity.polygon.hierarchy.getValue().positions,
|
||||
material: entity.polygon.material,
|
||||
classificationType: Cesium.ClassificationType.BOTH
|
||||
}
|
||||
//拆分边线,因为边线不能贴地,需要改为polyline,但是有可能存在entity本身就有polyline,所以需要单独创建一个entity,
|
||||
if (entity.polygon.outline.getValue()) {
|
||||
let positions = entity.polygon.hierarchy.getValue().positions
|
||||
let entity2 = new Cesium.Entity({
|
||||
id: this.getOutlineId(entity.id),
|
||||
polyline: {
|
||||
positions,
|
||||
width: entity.polygon.outlineWidth.getValue(),
|
||||
material: entity.polygon.outlineColor.getValue(),
|
||||
clampToGround: true,
|
||||
zIndex: this.sdk._entityZIndex
|
||||
}
|
||||
})
|
||||
this.source.entities.add(entity2)
|
||||
}
|
||||
entity.polygon = polygon
|
||||
}
|
||||
if (entity.billboard) {
|
||||
entity.billboard.heightReference =
|
||||
Cesium.HeightReference.CLAMP_TO_GROUND
|
||||
}
|
||||
if (entity.polyline) {
|
||||
entity.polyline = {
|
||||
positions: entity.polyline.positions.getValue(),
|
||||
material: entity.polyline.material,
|
||||
clampToGround: true,
|
||||
width: entity.polyline.width ? entity.polyline.width.getValue() : 1
|
||||
}
|
||||
//这里在设置贴地的时候,需要单独创建polyline,部分kml的polyline设置贴地,会导致引擎崩溃,原因暂未查询到
|
||||
}
|
||||
entity.show = true
|
||||
this.source.entities.add(entity)
|
||||
})
|
||||
await this.viewer.dataSources.add(this.source)
|
||||
}
|
||||
|
||||
getOutlineId(id) {
|
||||
return [id, 'outline'].join('_')
|
||||
}
|
||||
}
|
||||
|
||||
export default KML
|
98
src/Obj/Base/KML/index2.js
Normal file
98
src/Obj/Base/KML/index2.js
Normal file
@ -0,0 +1,98 @@
|
||||
// /**
|
||||
// * @name: kml
|
||||
// * @author: Administrator
|
||||
// * @date: 2023-07-19 09:25
|
||||
// * @description:kml
|
||||
// * @update: 2023-07-19 09:25
|
||||
// */
|
||||
// class KML2 {
|
||||
// constructor(options = {}) {
|
||||
// this.options = {...options}
|
||||
// this.viewer = YJ.getEarth().czm.viewer
|
||||
// this.setDefaultValue()
|
||||
// this.source = new Cesium.CustomDataSource(this.options.id)
|
||||
// this.billboardsCollection = this.viewer.scene.primitives.add(new Cesium.BillboardCollection());
|
||||
// this.labelCollection = this.viewer.scene.primitives.add(new Cesium.LabelCollection());
|
||||
|
||||
// // this.source = new Cesium.CompositeEntityCollection([])
|
||||
// this.detail = []
|
||||
// this.labelAttrs = ["show", "position", "text", "font", "fillColor"]
|
||||
// this.load()
|
||||
// }
|
||||
|
||||
// get show() {
|
||||
// return this.options.show
|
||||
// }
|
||||
|
||||
// set show(val) {
|
||||
// if (this.source) {
|
||||
// this.source.show = val
|
||||
// this.options.show = val
|
||||
// }
|
||||
// }
|
||||
|
||||
// setDefaultValue() {
|
||||
// this.options.id = this.options.id || Cesium.createGuid()
|
||||
// this.options.url = this.options.url || ""
|
||||
// this.options.show = this.options.show ?? true
|
||||
// }
|
||||
|
||||
// flyTo(duration = 3) {
|
||||
// if (this.source)
|
||||
// this.viewer.flyTo(this.source, {duration})
|
||||
// }
|
||||
|
||||
// remove() {
|
||||
// this.viewer.dataSources.remove(this.source);
|
||||
// this.source = null
|
||||
// }
|
||||
|
||||
// async load() {
|
||||
// this.show = this.options.show
|
||||
// let source = await Cesium.KmlDataSource.load(this.options.url,
|
||||
// {
|
||||
// camera: this.viewer.scene.camera,
|
||||
// canvas: this.viewer.scene.canvas,
|
||||
// // clampToGround: true
|
||||
// })
|
||||
// let types = ["label", "billboard", "polyline", "polygon"]//kml暂时只考虑这几种
|
||||
|
||||
// source.entities.values.forEach((entity, index) => {
|
||||
// if (entity.label) {
|
||||
// this.dealLabel(entity)
|
||||
// }
|
||||
// if (entity.billboard) {
|
||||
// this.addBillboard(entity)
|
||||
// }
|
||||
// })
|
||||
|
||||
// // await this.viewer.dataSources.add(this.source);
|
||||
// }
|
||||
|
||||
// getOutlineId(id) {
|
||||
// return [id, "outline"].join("_")
|
||||
// }
|
||||
|
||||
// addBillboard(entity) {
|
||||
// this.billboardsCollection.add({
|
||||
// position: entity.position.getValue(),
|
||||
// image: entity.billboard.image.getValue() //图片存储路径
|
||||
// });
|
||||
// }
|
||||
|
||||
// dealLabel(entity) {
|
||||
// let config = {
|
||||
// pixelOffset: new Cesium.Cartesian2(0, -32),
|
||||
// position: entity.position.getValue(),
|
||||
// }
|
||||
// this.labelAttrs.forEach(key => {
|
||||
// if (entity.label[key]) {
|
||||
// config[key] = entity.label[key].getValue()
|
||||
// }
|
||||
// })
|
||||
// this.labelCollection.add(config);
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// export default KML2
|
Reference in New Issue
Block a user