height(){
//默认测空间
this._reset();
console.log('1')
this._heightParams.heightDataSource = new Cesium.CustomDataSource('heightDataSource');
this.viewer.dataSources.add(this._heightParams.heightDataSource);
this.viewer.scene.globe.depthTestAgainstTerrain = true;//开启深度测试
this._height_handler = new Cesium.ScreenSpaceEventHandler(this.viewer.canvas);
console.log('1')
//绑定鼠标左击事件
//尝试过把下面的方法拿出来,但是总是找不到this对象。。。有缘人试试吧
this._height_handler.setInputAction((e) => {
console.log('1')
let pos = this.viewer.scene.pickPosition(e.position);//笛卡尔坐标
if (pos) {
this._heightParams.vertexCollection.push(pos);
}
//添加线
let currentLine = this._heightParams.heightDataSource.entities.add({
polyline: {
positions: new Cesium.CallbackProperty(() => {
let c = Array.from(this._heightParams.vertexCollection);
if (this._heightParams.mousePos) {
c.push(this._heightParams.mousePos);
}
return c
}, false),
clampToGround: true,//折线固定在地面
width: 3,
material: new Cesium.PolylineOutlineMaterialProperty({
color: new Cesium.Color(255 / 255, 118 / 255, 0 / 255, 1.0),
outlineColor: new Cesium.Color(255 / 255, 180 / 255, 115 / 255, 1.0),
outlineWidth: 2
})
}
})
if (this._heightParams.vertexCollection.length >= 2) {
this._heightParams.totalHeight += this._heightDistance(this._heightParams.vertexCollection[this._heightParams.vertexCollection.length - 2], pos);
this._heightParams.totalHeight = parseFloat((this._heightParams.totalHeight).toFixed(2));
}
this._addHeightPoint(pos, this._heightParams.totalHeight)
if (this._heightParams.lastLine) {
this._heightParams.heightDataSource.entities.remove(this._heightParams.lastLine)
}
this._heightParams.lastLine = currentLine;
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
//鼠标移动事件
this._height_handler.setInputAction((e) => {
console.log('1')
let endPosition = e.endPosition;
let endPos = this.viewer.scene.pickPosition(endPosition);
if (endPos) {//鼠标移出地球,undifined
this._heightParams.mousePos = endPos;
//entity polyline 中使用了Cesium CallbackProperty 免去移动再画线
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
//鼠标右键事件,右键取消不在添加点计算面积
this._height_handler.setInputAction((e) => {
console.log('1')
let endPostion = e.position;
let endPos = this.viewer.scene.pickPosition(endPostion);
if (endPos) {
this.cancel();
// vertexCollection.push(endPos);
// if(type==0){
// totalHeight+=this._surfaceDistance(vertexCollection[vertexCollection.length-2],endPos);
// this._addDistancePoint(endPos,totalHeight);
// }else{
// let left = vertexCollection[vertexCollection.length-2];
// let right = endPos;
// //求两个顶点的直线距离,用于判断插值精度
// let t = Cesium.Cartesian3.distance(left,right);
// let vertexLength = this._terrianDistance(vertexCollection[vertexCollection.length-2],endPos,t);
// totalHeight+=vertexLength;
// this._addDistancePoint(endPos,totalHeight);
// }
this._heightParams.heightDataSource.entities.add({
polyline: {
clampToGround: true,
positions: this._heightParams.vertexCollection,
width: 3,
material: new Cesium.PolylineOutlineMaterialProperty({
color: new Cesium.Color(255 / 255, 118 / 255, 0 / 255, 1.0),
outlineColor: new Cesium.Color(255 / 255, 180 / 255, 115 / 255, 1.0),
outlineWidth: 2
})
}
})
if (this._heightParams.lastLine) {
this._heightParams.heightDataSource.entities.remove(this._heightParams.lastLine);
}
this._heightParams.vertexCollection = [];
this._heightParams.mousePos = null;
}
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
}
/**
* 空间距离计算
* @param start
* @param end
* @returns {number}
* @private
/
_heightDistance(start, end) {
console.log('1')
let geodesic = new Cesium.EllipsoidGeodesic();
let startGeodesic = Cesium.Cartographic.fromCartesian(start);
let endGeodesic = Cesium.Cartographic.fromCartesian(end);
geodesic.setEndPoints(startGeodesic.height, endGeodesic.height);
let heightDiff = Math.abs(endGeodesic.height - startGeodesic.height);
return heightDiff;
}
/*
* 添加顶点长度信息等
* @param pos
* @param totalLength
* @private
*/
_addHeightPoint(pos, totalLength) {
this._heightParams.heightDataSource.entities.add({
position: pos,
point: {
color: new Cesium.Color(93 / 255, 200 / 255, 205 / 255, 1.0),
pixelSize: 10,
heightReference: Cesium.CLAMP_TO_GROUND,
disableDepthTestDistance: 15000000
},
label: {
text: totalLength + "千米",
fillColor: Cesium.Color.WHITE,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 2,
pixelOffset: new Cesium.Cartesian2(10, -10),
font: 'normal 16px',
disableDepthTestDistance: 15000000
}
})
}将这段代码改为测量高度