939dec7d by jikai

111111

1 parent 4248ae6a
export default {
measureTools(viewer) {
var entityCollection = [];
this.getCollection = function () {
return entityCollection;
};
/**
* 清除
*/
this.destroy = function () {
for (var i = 0; i < entityCollection.length; i++) {
viewer.entities.remove(entityCollection[i]);
}
entityCollection = [];
};
/**
* 测距
*/
this.measurePolyLine = function () {
var positions = [];
var labelEntity = null; // 标签实体
// 注册鼠标左击事件
viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) {
var cartesian = viewer.scene.pickPosition(clickEvent.position); // 坐标
// 存储第一个点
if (positions.length == 0) {
positions.push(cartesian.clone());
addPoint(cartesian);
// 注册鼠标移动事件
viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) {
var movePosition = viewer.scene.pickPosition(moveEvent.endPosition); // 鼠标移动的点
if (positions.length == 2) {
positions.pop();
positions.push(movePosition);
// 绘制label
if (labelEntity) {
viewer.entities.remove(labelEntity);
entityCollection.splice(entityCollection.indexOf(labelEntity), 1);
}
// 计算中点
var centerPoint = Cesium.Cartesian3.midpoint(positions[0], positions[1], new Cesium.Cartesian3());
// 计算距离
var lengthText = "距离:" + getLengthText(positions[0], positions[1]);
labelEntity = addLabel(centerPoint, lengthText);
entityCollection.push(labelEntity);
} else {
positions.push(movePosition);
// 绘制线
addLine(positions);
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
} else {
// 存储第二个点
positions.pop();
positions.push(cartesian);
addPoint(cartesian);
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
};
/**
* 测面积
*/
this.measurePolygon = function () {
var positions = [];
var clickStatus = false;
var labelEntity = null;
viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) {
clickStatus = true;
// var cartesian = viewer.scene.pickPosition(clickEvent.position);
var cartesian = viewer.scene.globe.pick(viewer.camera.getPickRay(clickEvent.position), viewer.scene);
if (positions.length == 0) {
positions.push(cartesian.clone()); //鼠标左击 添加第1个点
addPoint(cartesian);
viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) {
// var movePosition = viewer.scene.pickPosition(moveEvent.endPosition);
var movePosition = viewer.scene.globe.pick(viewer.camera.getPickRay(moveEvent.endPosition), viewer.scene);
if (positions.length == 1) {
positions.push(movePosition);
addLine(positions);
} else {
if (clickStatus) {
positions.push(movePosition);
} else {
positions.pop();
positions.push(movePosition);
}
}
if (positions.length >= 3) {
// 绘制label
if (labelEntity) {
viewer.entities.remove(labelEntity);
entityCollection.splice(entityCollection.indexOf(labelEntity), 1);
}
var text = "面积:" + getArea(positions);
var centerPoint = getCenterOfGravityPoint(positions);
labelEntity = addLabel(centerPoint, text);
entityCollection.push(labelEntity);
}
clickStatus = false;
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
} else if (positions.length == 2) {
positions.pop();
positions.push(cartesian.clone()); // 鼠标左击 添加第2个点
addPoint(cartesian);
addPolyGon(positions);
// 右击结束
viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) {
// var clickPosition = viewer.scene.pickPosition(clickEvent.position);
var clickPosition = viewer.scene.globe.pick(viewer.camera.getPickRay(clickEvent.position), viewer.scene);
// console.log(clickPosition);
positions.pop();
positions.push(clickPosition);
positions.push(positions[0]); // 闭合
addPoint(clickPosition);
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
} else if (positions.length >= 3) {
positions.pop();
positions.push(cartesian.clone()); // 鼠标左击 添加第3个点
addPoint(cartesian);
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
};
/**
* 测高
*/
this.measureHeight = function () {
var positions = [];
var labelEntity_1 = null; // 标签实体
var labelEntity_2 = null; // 标签实体
var labelEntity_3 = null; // 标签实体
// 注册鼠标左击事件
viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) {
var cartesian = viewer.scene.pickPosition(clickEvent.position); // 坐标
// 存储第一个点
if (positions.length == 0) {
positions.push(cartesian.clone());
addPoint(cartesian);
// 注册鼠标移动事件
viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) {
var movePosition = viewer.scene.pickPosition(moveEvent.endPosition); // 鼠标移动的点
if (positions.length >= 2) {
positions.pop();
positions.pop();
positions.pop();
var cartographic = Cesium.Cartographic.fromCartesian(movePosition);
var height = Cesium.Cartographic.fromCartesian(positions[0]).height;
var verticalPoint = Cesium.Cartesian3.fromDegrees(Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude), height);
positions.push(verticalPoint);
positions.push(movePosition);
positions.push(positions[0]);
// 绘制label
if (labelEntity_1) {
viewer.entities.remove(labelEntity_1);
entityCollection.splice(entityCollection.indexOf(labelEntity_1), 1);
viewer.entities.remove(labelEntity_2);
entityCollection.splice(entityCollection.indexOf(labelEntity_2), 1);
viewer.entities.remove(labelEntity_3);
entityCollection.splice(entityCollection.indexOf(labelEntity_3), 1);
}
// 计算中点
var centerPoint_1 = Cesium.Cartesian3.midpoint(positions[0], positions[1], new Cesium.Cartesian3());
// 计算距离
var lengthText_1 = "水平距离:" + getLengthText(positions[0], positions[1]);
labelEntity_1 = addLabel(centerPoint_1, lengthText_1);
entityCollection.push(labelEntity_1);
// 计算中点
var centerPoint_2 = Cesium.Cartesian3.midpoint(positions[1], positions[2], new Cesium.Cartesian3());
// 计算距离
var lengthText_2 = "垂直距离:" + getLengthText(positions[1], positions[2]);
labelEntity_2 = addLabel(centerPoint_2, lengthText_2);
entityCollection.push(labelEntity_2);
// 计算中点
var centerPoint_3 = Cesium.Cartesian3.midpoint(positions[2], positions[3], new Cesium.Cartesian3());
// 计算距离
var lengthText_3 = "直线距离:" + getLengthText(positions[2], positions[3]);
labelEntity_3 = addLabel(centerPoint_3, lengthText_3);
entityCollection.push(labelEntity_3);
} else {
var verticalPoint = new Cesium.Cartesian3(movePosition.x, movePosition.y, positions[0].z);
positions.push(verticalPoint);
positions.push(movePosition);
positions.push(positions[0]);
// 绘制线
addLine(positions);
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
} else {
// 存储第二个点
positions.pop();
positions.pop();
positions.pop();
var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
var height = Cesium.Cartographic.fromCartesian(positions[0]).height;
var verticalPoint = Cesium.Cartesian3.fromDegrees(Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude), height);
positions.push(verticalPoint);
positions.push(cartesian);
positions.push(positions[0]);
addPoint(cartesian);
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
};
/**
* 添加点
* @param position
*/
var addPoint = function (position) {
entityCollection.push(viewer.entities.add(new Cesium.Entity({
position: position,
point: {
// color: Cesium.Color.SKYBLUE,
// pixelSize: 8,
// outlineColor: Cesium.Color.YELLOW,
// outlineWidth: 2,
// disableDepthTestDistance: Number.POSITIVE_INFINITY
pixelSize: 5,
color: Cesium.Color.YELLOW,
outlineWidth: 2,
outlineColor: Cesium.Color.DARKRED,
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
})));
};
/**
* 添加线
* @param positions
*/
var addLine = function (positions) {
entityCollection.push(viewer.entities.add(new Cesium.Entity({
polyline: {
positions: new Cesium.CallbackProperty(function () {
return positions
}, false),
perPositionHeight: true,
show: true,
material: Cesium.Color.YELLOW,
width: 3,
clampToGround: true
}
})));
};
/**
* 添加面
* @param positions
*/
var addPolyGon = function (positions) {
var dynamicPositions = new Cesium.CallbackProperty(function () {
return new Cesium.PolygonHierarchy(positions);
}, false);
entityCollection.push(viewer.entities.add(new Cesium.Entity({
polygon: {
hierarchy: dynamicPositions,
material: new Cesium.Color.fromCssColorString("#FFD700").withAlpha(.2),
perPositionHeight: true,
width: 3,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 3,
outline: true
// material: Cesium.Color.RED.withAlpha(0.6),
// classificationType: Cesium.ClassificationType.BOTH // 贴地表和贴模型,如果设置了,这不能使用挤出高度
}
})));
};
/**
* 添加标签
* @param position
* @param text
*/
var addLabel = function (centerPoint, text) {
return viewer.entities.add(new Cesium.Entity({
position: centerPoint,
label: {
text: text,
font: '12pt sans-serif',
style: Cesium.LabelStyle.FILL_AND_OUTLINE, //FILL FILL_AND_OUTLINE OUTLINE
fillColor: Cesium.Color.YELLOW,
showBackground: true,//指定标签后面背景的可见性
backgroundColor: new Cesium.Color(0.165, 0.165, 0.165, 0.8), // 背景颜色
backgroundPadding: new Cesium.Cartesian2(6, 6),//指定以像素为单位的水平和垂直背景填充padding
pixelOffset: new Cesium.Cartesian2(0, -25)
}
}));
};
/**
* 计算两点距离
* @param firstPoint
* @param secondPoint
*/
var getLengthText = function (firstPoint, secondPoint) {
// 计算距离
var length = Cesium.Cartesian3.distance(firstPoint, secondPoint);
if (length > 1000) {
length = (length / 1000).toFixed(2) + " 公里";
} else {
length = length.toFixed(2) + " 米";
}
return length;
};
//计算多边形面积
var getArea = function (points) {
var radiansPerDegree = Math.PI / 180.0;//角度转化为弧度(rad)
var degreesPerRadian = 180.0 / Math.PI;//弧度转化为角度
/*角度*/
function Angle(p1, p2, p3) {
var bearing21 = Bearing(p2, p1);
var bearing23 = Bearing(p2, p3);
var angle = bearing21 - bearing23;
if (angle < 0) {
angle += 360;
}
return angle;
}
/*方向*/
function Bearing(from, to) {
from = Cesium.Cartographic.fromCartesian(from);
to = Cesium.Cartographic.fromCartesian(to);
var lat1 = from.latitude;
var lon1 = from.longitude;
var lat2 = to.latitude;
var lon2 = to.longitude;
var angle = -Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2));
if (angle < 0) {
angle += Math.PI * 2.0;
}
angle = angle * degreesPerRadian;//角度
return angle;
}
function distance(point1, point2) {
var point1cartographic = Cesium.Cartographic.fromCartesian(point1);
var point2cartographic = Cesium.Cartographic.fromCartesian(point2);
/**根据经纬度计算出距离**/
var geodesic = new Cesium.EllipsoidGeodesic();
geodesic.setEndPoints(point1cartographic, point2cartographic);
var s = geodesic.surfaceDistance;
//console.log(Math.sqrt(Math.pow(distance, 2) + Math.pow(endheight, 2)));
//返回两点之间的距离
s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
return s;
}
var res = 0;
//拆分三角曲面
for (var i = 0; i < points.length - 2; i++) {
var j = (i + 1) % points.length;
var k = (i + 2) % points.length;
var totalAngle = Angle(points[i], points[j], points[k]);
var dis_temp1 = distance(points[j], points[0]);
var dis_temp2 = distance(points[k], points[0]);
res += dis_temp1 * dis_temp2 * Math.sin(totalAngle) / 2;
// console.log(res);
}
if (res < 1000000) {
res = Math.abs(res).toFixed(4) + " 平方米";
} else {
res = Math.abs((res / 1000000.0).toFixed(4)) + " 平方公里";
}
return res;
};
/**
* 计算多边形的中心(简单的处理)
* @param mPoints
* @returns {*[]}
*/
var getCenterOfGravityPoint = function (mPoints) {
var centerPoint = mPoints[0];
for (var i = 1; i < mPoints.length; i++) {
centerPoint = Cesium.Cartesian3.midpoint(centerPoint, mPoints[i], new Cesium.Cartesian3());
}
return centerPoint;
}
}
}
\ No newline at end of file
export default {
movePrompt: class MovePrompt {
constructor(viewer, opt) {
if (!opt) opt = {};
var randomId = Number((new Date()).getTime());
this.id = randomId;
this.style = opt.style;
this.viewer = viewer;
if (!this.viewer) return;
this.scene = this.viewer.scene;
this.camera = this.viewer.camera;
this.mapContainer = this.viewer.container.id;
this.rendHandler = null;
if (!this.mapContainer) return;
this.trackPopUpId = "trackPopUp" + randomId;
this.promptContentId = "promptContent" + randomId;
this.promptDivId = "promptDiv" + randomId;
this.trackPopUpContentId = "trackPopUpContent" + randomId;
this.closeBtnId = "closeBtn" + randomId;
var infoDiv;
var max_width = 300;
var max_height = 500;
infoDiv = window.document.createElement("div");
infoDiv.id = this.trackPopUpId;
infoDiv.className = "trackPopUp";
this.content = opt.content || ""; //提示框内容
if (!opt.type || opt.type == 1) {
infoDiv.innerHTML = '<div id="' + this.trackPopUpContentId + '" class="cesium-popup" style="top:0;left:0;">' +
'<div class="cesium-prompt-content-wrapper" id="' + this.promptDivId + '">' +
'<div id="trackPopUpLink" class="cesium-popup-content" style="max-width: ' + max_width + 'px;max-height: ' + max_height + 'px">' +
'<span class="promptContent" id="' + this.promptContentId + '">' + this.content + '</span>' +
'</div>' +
'</div>' +
'</div>';
} else {
infoDiv.innerHTML = '<div id="' + this.trackPopUpContentId + '" class="cesium-popup" style="top:0;left:0;">' +
'<a class="cesium-popup-close-button" href="javascript:void(0)" id="' + this.closeBtnId + '">×</a>' +
'<div class="cesium-popup-content-wrapper" id="' + this.promptDivId + '">' +
'<div id="trackPopUpLink" class="cesium-popup-content" style="max-width: ' + max_width + 'px;max-height: ' + max_height + 'px">' +
'<span class="popupContent" id="' + this.promptContentId + '">' + this.content + '</span>' +
'</div>' +
'</div>' +
'<div class="cesium-popup-tip-container">' +
'<div class="cesium-popup-tip"></div>' +
'</div>' +
'</div>';
}
window.document.getElementById(this.mapContainer).appendChild(infoDiv);
window.document.getElementById(this.trackPopUpId).style.display = "block";
this.offset = opt.offset || {};
this.infoDiv = window.document.getElementById(this.trackPopUpId);
this.trackPopUpContent = window.document.getElementById(this.trackPopUpContentId);
this.promptDiv = window.document.getElementById(this.promptDivId);
this.promptContent = window.document.getElementById(this.promptContentId);
this.trackPopUpLink = window.document.getElementById(this.promptContentId);
this.popupCartesian = opt.popupCartesian;
this.rendHandler = null;
this.show = (opt.show == undefined ? true : opt.show);
if(opt.type==2){
if(!this.popupCartesian){
console.warn("缺少空间坐标!");
return ;
}
}
if (opt.type && opt.type != 1 && this.popupCartesian) {
var clsBtn = window.document.getElementById(this.closeBtnId);
var that = this;
clsBtn.addEventListener("click", function () {
that.setVisible(false);
});
this.rendHandler = this.viewer.scene.postRender.addEventListener(function () {
if (that.popupCartesian) {
var px = Cesium.SceneTransforms.wgs84ToWindowCoordinates(that.scene, that.popupCartesian);
that.trackPopUpContent.style.left = px.x + (that.offset.x || 0)+ (Math.ceil(-that.trackPopUpContent.offsetWidth / 2)) + "px";
that.trackPopUpContent.style.top = px.y + (that.offset.y ||0)+ (-that.trackPopUpContent.offsetHeight) + "px";
var res = false;
var e = that.popupCartesian,
i = that.camera.position,
n = that.scene.globe.ellipsoid.cartesianToCartographic(i).height;
if (!(n += 1 * that.scene.globe.ellipsoid.maximumRadius, Cesium.Cartesian3.distance(i, e) > n)) {
res = true;
}
if (res && that.show) {
if (that.infoDiv) that.infoDiv.style.display = "block";
} else {
if (that.infoDiv) that.infoDiv.style.display = "none";
}
}
});
}
}
setHtml(html) {
if (!html) {
return;
}
if (this.trackPopUpLink) {
this.trackPopUpLink.innerText = html;
}
}
destroy() {
if (this.infoDiv && this.mapContainer) {
this.infoDiv.style.display = "none";
window.document.getElementById(this.mapContainer).removeChild(this.infoDiv);
this.infoDiv = null;
}
if (this.rendHandler) {
this.rendHandler();
this.rendHandler = null;
}
}
displayPrompt(display) {
if (this.infoDiv) this.infoDiv.style.display = display ? "block" : "none";
}
updateStyle(opt) {
if (!opt) opt = {};
this.promptDiv.style.background = opt.rgba || "rgba(0,0,0,.4)";
this.promptContent.style.color = opt.fontColor || "white";
}
updatePrompt(px, html) {
if (!px) return;
this.infoDiv.style.display = "block";
this.trackPopUpContent.style.left = px.x + (this.offset.x || 30) + "px";
this.trackPopUpContent.style.top = px.y + (this.offset.y || 30) + "px";
this.setHtml(html);
}
setVisible(isOpen) {
if(isOpen==undefined) isOpen = true;
if(isOpen){
this.infoDiv.style.display = "block";
this.show = true;
}else{
this.infoDiv.style.display = "none";
this.show = false;
}
}
}
}
\ No newline at end of file
export default {
createPoint(opt) {
return this.viewer.entities.add({
position: opt.position,
point: opt.point || {
pixelSize: 5,
color: Cesium.Color.YELLOW,
outlineWidth: 2,
outlineColor: Cesium.Color.DARKRED,
disableDepthTestDistance: Number.POSITIVE_INFINITY
},
show: false
});
},
createPolyline(positions) {
let style = this.style || {};
var polyline = this.viewer.entities.add({
polyline: {
positions: new Cesium.CallbackProperty(() => {
return positions || this.positions
}, false),
show: true,
material: style.material || Cesium.Color.YELLOW,
width: style.width || 3,
clampToGround: style.clampToGround == undefined ? false : true
}
});
polyline.objId = this.objId;
return polyline;
},
createPolygon() {
return this.viewer.entities.add({
polygon: {
hierarchy: new Cesium.CallbackProperty(() => {
return new Cesium.PolygonHierarchy(this.positions)
}, false),
material: new Cesium.Color.fromCssColorString("#FFD700").withAlpha(.2),
perPositionHeight: true,
width: 3,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 3,
outline: true
}
});
},
createRectangle() {
var that = this;
var rectangle = this.viewer.entities.add({
rectangle: {
coordinates: new Cesium.CallbackProperty(() => {
return Cesium.Rectangle.fromCartesianArray([this.leftup, this.rightdown])
}, false),
material: new Cesium.Color.fromCssColorString("#FFD700").withAlpha(.2),
perPositionHeight: true,
width: 3,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 3,
outline: true
}
});
rectangle.objId = this.objId;
return rectangle;
},
createCircle() {
var ellipse = this.viewer.entities.add({
position: this.center,
ellipse: {
semiMajorAxis: new Cesium.CallbackProperty(() => {
return this.radius
}, false),
semiMinorAxis: new Cesium.CallbackProperty(() => {
return this.radius
}, false),
material: new Cesium.Color.fromCssColorString("#FFD700").withAlpha(.2),
perPositionHeight: true,
width: 3,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 3,
outline: true
}
});
ellipse.objId = this.objId;
return ellipse;
}
}
\ No newline at end of file
import movePrompt from './MovePrompt';
export default class CreateBillboard {
constructor(viewer, style) {
this.objId = Number((new Date()).getTime() + "" + Number(Math.random() * 1000).toFixed(0));
this.viewer = viewer;
this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.state = 0; //1为新增 2为编辑 0为清除
this.image = style.image || "content/images/map3d/mark1.png";
this.style = style;
this.billboardArg = {};
this.billboard = null;
this.openModify = false;
if (style) {
var haveImage = false;
for (var i in style) {
if (i == "image" && style[i]) {
haveImage = true;
}
if (style[i]) this.billboardArg[i] = style[i];
}
} else {
console.warn("未设置billboard的参数!");
return;
}
this.prompt = new movePrompt.movePrompt(viewer);
}
start(callBack) {
var that = this;
this.handler.setInputAction(function(evt) { //单机开始绘制
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer,[]);
that.billboard = that.createBillboard(cartesian);
that.state = 1;
that.handler.destroy();
if (that.prompt) {
that.prompt.destroy();
that.prompt = null;
}
if (callBack) callBack(that.billboard);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
this.handler.setInputAction(function(evt) { //单机开始绘制
that.prompt.updatePrompt(evt.endPosition, "单击新增");
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
}
createByPositions(lnglatArr, callBack) {
if (!lnglatArr) return;
var position = Cesium.Cartesian3.fromDegrees(lnglatArr[0], lnglatArr[1],lnglatArr[2]);
if (!position) return;
this.billboard = this.createBillboard(position);
callBack(this.billboard);
this.state = 1;
}
startModify() {
if (this.state != 2 && this.state != 1) return; //表示还没绘制完成
if (!this.modifyHandler) this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
var that = this;
this.modifyHandler.setInputAction(function(evt) {
var pick = that.viewer.scene.pick(evt.position);
if (Cesium.defined(pick) && pick.id) {
if (!pick.id.objId)
that.forbidDrawWorld(true);
that.openModify = true;
that.forbidDrawWorld(true);
that.state = 2;
} else {
//if(that.modifyPrompt) that.modifyPrompt.destroy();
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
this.modifyHandler.setInputAction(function(evt) { //移动时绘制线
if (!that.billboard || !that.openModify) return;
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer,[]);
if (that.billboard) {
//that.modifyPrompt.updatePrompt(evt.endPosition, "鼠标拖动修改");
that.billboard.position.setValue(cartesian);
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.modifyHandler.setInputAction(function(evt) { //移动时绘制线
if (!that.billboard || !that.openModify) return;
that.openModify = false;
that.forbidDrawWorld(false);
if (that.modifyHandler) {
that.modifyHandler.destroy();
that.modifyHandler = null;
}
}, Cesium.ScreenSpaceEventType.LEFT_UP);
}
endModify(callback) {
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
if (callback) callback(this.billboard);
}
this.state = 2;
}
createBillboard(cartesian) {
if (!cartesian) return;
var billboard = this.viewer.entities.add({
position: cartesian,
billboard: {
image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAABMElEQVRIS+2VO0oEQRCGv38RBdFDGQumgijeQFTMjDUQr6CRsgeQDfQCmoqBZkYeQF0f+Cop6AYfa8/UOGtkw8Aw0/19XVVNlxjyUIlvZlPAGjAn6brJXn4UmNk4cJeeDtAFtiWdR0QlgUNfgDzH35+BU2AT6El6q5JFBB9ZN8ADsAXslNLXVJBlnkKPdB9YkdT/GtFvBZnnqepI+sZrQ+C1GQEWJO21GcEr8AQcAquSLgcVvEkElgp8AixLOiudpKjAi3gBLEk6rjqi/r+uwMFXCXxUB5zn1BHcJ/BuBBwR+B00L6n3L/iUATMbAx7TxxlJB62myGFmtgjMAtOS/PyHR7HhhGkDFvy9wMw2UpscDUTgd5J3u/XKy87MboGJADxP7UuarCMYbgQNdl5c8g7JKYAZpEkyjAAAAABJRU5ErkJggg==',
heightReference: this.style.heightReference || Cesium.HeightReference.NONE,
disableDepthTestDistance:Number.MAX_VALUE,
color: Cesium.Color.RED
}
})
billboard.objId = this.objId;
return billboard;
}
getPositions() {
return this.billboard.position;
}
setStyle(obj) {}
remove() {
if (this.billboard) {
this.state = 0;
this.viewer.entities.remove(this.billboard);
this.billboard = null;
}
}
setVisible(vis) {
this.billboard.show = vis;
}
forbidDrawWorld(isForbid) {
this.viewer.scene.screenSpaceCameraController.enableRotate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTilt = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTranslate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableInputs = !isForbid;
}
destroy() {
this.openModify = false;
if (this.handler) {
this.handler.destroy();
this.handler = null;
}
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
}
if (this.billboard) {
this.viewer.entities.remove(this.billboard);
this.billboard = null;
}
this.style = null;
if (this.prompt) {
that.prompt.destroy();
this.prompt = null;
}
}
getCatesian3FromPX(px, viewer, entitys) {
var picks = viewer.scene.drillPick(px);
this.viewer.scene.render();
var cartesian;
var isOn3dtiles = false;
for (var i = 0; i < picks.length; i++) {
var isContinue = false;
for (var step = 0; step < entitys.length; step++) {
if (entitys[step] && picks[i].id && entitys[step].objId == picks[i].id.objId) {
isContinue = true;
break;
}
}
if (isContinue) continue;
if ((picks[i] && picks[i].primitive) || picks[i] instanceof Cesium.Cesium3DTileFeature) { //模型上拾取
isOn3dtiles = true;
}
}
if (isOn3dtiles) {
cartesian = viewer.scene.pickPosition(px);
} else {
var ray = viewer.camera.getPickRay(px);
if (!ray) return null;
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
}
return cartesian;
}
}
\ No newline at end of file
import movePrompt from './MovePrompt';
import addCompany from './addCompany';
export default class CreateCircle {
constructor(viewer, style) {
this.objId = Number((new Date()).getTime() + "" + Number(Math.random() * 1000).toFixed(0));
this.viewer = viewer;
this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.circle = null;
this.screenSpaceCameraController = [];
this.style = style;
this.floatPoint = null;
this.centerPoint = null;
this.center = null;
this.float = null;
this.radius = 0;
this.modifyPoint = null;
this.state = 0;
this.pointArr = [];
this.prompt = new movePrompt.movePrompt(viewer);
}
start(callBack) {
var that = this;
this.handler.setInputAction(function (evt) { //单机开始绘制
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.circle]);
if (!that.centerPoint) {
that.center = cartesian;
that.centerPoint = addCompany.createPoint.call(that, {position: cartesian});
that.centerPoint.typeAttr = "center";
that.floatPoint = addCompany.createPoint.call(that, {position: cartesian.clone()});
that.float = cartesian.clone();
that.floatPoint.typeAttr = "float";
that.circle = addCompany.createCircle.call(that);
} else {
return;
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
this.handler.setInputAction(function (evt) { //移动时绘制线
if (!that.centerPoint) {
that.prompt.updatePrompt(evt.endPosition, "单击开始绘制");
return;
}
that.prompt.updatePrompt(evt.endPosition, "右键结束");
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.circle]);
if (that.floatPoint) {
that.floatPoint.position.setValue(cartesian);
that.float = cartesian.clone();
}
that.radius = Cesium.Cartesian3.distance(cartesian, that.center);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.handler.setInputAction(function () { //单机开始绘制
if (!that.circle) {
return;
}
that.state = 1;
that.handler.destroy();
if (that.floatPoint) that.floatPoint.show = false;
if (that.centerPoint) that.centerPoint.show = false;
if (that.prompt) {
that.prompt.destroy();
that.prompt = null;
}
if (callBack) callBack(that.circle);
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}
startModify(callback) {
if (this.state != 2 && this.state != 1) return; //表示还没绘制完成
if (!this.modifyHandler) this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
var that = this;
if (that.floatPoint) that.floatPoint.show = true;
if (that.centerPoint) that.centerPoint.show = true;
this.modifyHandler.setInputAction(function (evt) {
if (!that.circle) return;
var pick = that.viewer.scene.pick(evt.position);
if (Cesium.defined(pick) && pick.id) {
if (!pick.id.objId)
that.modifyPoint = pick.id;
that.forbidDrawWorld(true);
} else {
if (that.floatPoint) that.floatPoint.show = false;
if (that.centerPoint) that.centerPoint.show = false;
if (that.modifyHandler) {
that.modifyHandler.destroy();
that.modifyHandler = null;
if (callback) callback(that.circle);
}
that.state = 2;
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
this.modifyHandler.setInputAction(function (evt) {
if (!that.modifyPoint) return;
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.circle]);
if (!cartesian) {
return;
}
if (that.modifyPoint.typeAttr == "center") {
that.center = cartesian
that.centerPoint.position.setValue(that.center);
that.circle.position.setValue(that.center);
} else {
that.float = cartesian
that.floatPoint.position.setValue(that.float);
}
that.radius = Cesium.Cartesian3.distance(that.float, that.center);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.modifyHandler.setInputAction(function (evt) {
if (!that.modifyPoint) return;
that.modifyPoint = null;
that.forbidDrawWorld(false);
}, Cesium.ScreenSpaceEventType.LEFT_UP);
}
endModify(callback) {
if (this.floatPoint) this.floatPoint.show = false;
if (this.centerPoint) this.centerPoint.show = false;
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
if (callback) callback(this.circle);
}
this.state = 2;
}
getCenter() {
return this.center;
}
getAttr() {
return obj;
}
setStyle(obj) {}
remove() {
if (this.circle) {
this.state = 0;
this.viewer.entities.remove(this.circle);
this.circle = null;
}
}
setVisible(vis) {
this.circle.show = vis;
}
forbidDrawWorld(isForbid) {
this.viewer.scene.screenSpaceCameraController.enableRotate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTilt = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTranslate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableInputs = !isForbid;
}
destroy() {
if (this.handler) {
this.handler.destroy();
this.handler = null;
}
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
}
if (this.circle) {
this.viewer.entities.remove(this.circle);
this.circle = null;
}
if (this.floatPoint) {
this.viewer.entities.remove(this.floatPoint);
this.floatPoint = null;
}
if (this.centerPoint) {
this.viewer.entities.remove(this.centerPoint);
this.centerPoint = null;
}
this.style = null;
this.modifyPoint = null;
if (this.prompt) this.prompt.destroy();
}
getCatesian3FromPX(px, viewer, entitys) {
var picks = viewer.scene.drillPick(px);
this.viewer.scene.render();
var cartesian;
var isOn3dtiles = false;
for (var i = 0; i < picks.length; i++) {
var isContinue = false;
for (var step = 0; step < entitys.length; step++) {
if (entitys[step] && picks[i].id && entitys[step].objId == picks[i].id.objId) {
isContinue = true;
break;
}
}
if (isContinue) continue;
if ((picks[i] && picks[i].primitive) || picks[i] instanceof Cesium.Cesium3DTileFeature) { //模型上拾取
isOn3dtiles = true;
}
}
if (isOn3dtiles) {
cartesian = viewer.scene.pickPosition(px);
} else {
var ray = viewer.camera.getPickRay(px);
if (!ray) return null;
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
}
return cartesian;
}
}
\ No newline at end of file
import movePrompt from './MovePrompt';
import addCompany from './addCompany';
export default class CreatePolygon {
constructor(viewer, style) {
this.objId = Number((new Date()).getTime() + "" + Number(Math.random() * 1000).toFixed(0));
this.viewer = viewer;
this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.polygon = null;
this.polyline = null;
this.positions = [];
this.style = style;
this.state = 0; //1为新增 2为编辑 0为清除
this.gonPointArr = [];
this.modifyPoint = null;
this.prompt = new movePrompt.movePrompt(viewer);
}
start(callBack) {
var that = this;
this.handler.setInputAction(function(evt) { //单机开始绘制
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.polygon]);
if (that.positions.length == 0) {
that.positions.push(cartesian.clone());
}
that.positions.push(cartesian);
var point = addCompany.createPoint.call(that, {position: cartesian});
point.wz = that.gonPointArr.length;
that.gonPointArr.push(point);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
this.handler.setInputAction(function(evt) { //移动时绘制面
if (that.positions.length < 1) {
that.prompt.updatePrompt(evt.endPosition, "单击开始绘制");
return;
}
that.prompt.updatePrompt(evt.endPosition, "单击新增,右键结束");
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.polygon]);
if (that.positions.length == 2) {
if (!Cesium.defined(that.polyline)) that.polyline = addCompany.createPolyline.call(that);
} else {
if (!Cesium.defined(that.polygon)) {
that.polygon = addCompany.createPolygon.call(that);
that.polygon.isFilter = true;
that.polygon.objId = that.objId;
if (that.polyline) that.viewer.entities.remove(that.polyline);
}
}
that.positions.pop();
that.positions.push(cartesian);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.handler.setInputAction(function(evt) {
if (!that.polygon) return;
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.polygon]);
that.state = 1;
that.handler.destroy();
if (that.floatPoint) {
if (that.floatPoint) that.floatPoint.show = false;
that.floatPoint = null;
}
that.positions.pop();
that.positions.push(cartesian);
var point = addCompany.createPoint.call(that, {position: cartesian});
point.wz = that.gonPointArr.length;
that.gonPointArr.push(point);
if (that.prompt) {
that.prompt.destroy();
that.prompt = null;
}
callBack(that.polygon);
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}
createByPositions(lnglatArr, callBack) {
if (!lnglatArr) return;
var positions = that.getCatesian3FromPX.lnglatArrToCartesianArr(lnglatArr);
if (!positions) return;
this.polygon = addCompany.createPolygon.call(this);
this.positions = positions;
callBack(this.polygon);
for (var i = 0; i < positions.length; i++) {
var point = addCompany.createPoint.call(that, {position: positions[i]});
point.isFilter = true;
point.wz = this.gonPointArr.length;
this.gonPointArr.push(point);
}
this.state = 1;
this.polygon.objId = this.objId;
}
startModify() {
if (this.state != 1 && this.state != 2) return; //表示还没绘制完成
if (!this.modifyHandler) this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
var that = this;
for (var i = 0; i < that.gonPointArr.length; i++) {
var point = that.gonPointArr[i];
if (point) point.show = true;
}
this.modifyHandler.setInputAction(function(evt) {
var pick = that.viewer.scene.pick(evt.position);
if (Cesium.defined(pick) && pick.id) {
if (!pick.id.objId)
that.modifyPoint = pick.id;
that.forbidDrawWorld(true);
} else {
for (var i = 0; i < that.gonPointArr.length; i++) {
var point = that.gonPointArr[i];
if (point) point.show = false;
}
if (that.modifyHandler) {
that.modifyHandler.destroy();
that.modifyHandler = null;
}
that.state = 2;
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
this.modifyHandler.setInputAction(function(evt) { //移动时绘制面
if (that.positions.length < 1 || !that.modifyPoint) return;
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.polygon, that.modifyPoint]);
if (cartesian) {
that.modifyPoint.position.setValue(cartesian);
that.positions[that.modifyPoint.wz] = cartesian;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.modifyHandler.setInputAction(function(evt) {
that.forbidDrawWorld(false);
if (!that.modifyPoint) return;
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.polygon, that.modifyPoint]);
that.modifyPoint.position.setValue(cartesian);
that.positions[that.modifyPoint.wz] = cartesian;
that.modifyPoint = null;
that.forbidDrawWorld(false);
}, Cesium.ScreenSpaceEventType.LEFT_UP);
}
endModify(callback) {
for (var i = 0; i < this.gonPointArr.length; i++) {
var point = this.gonPointArr[i];
if (point) point.show = false;
}
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
}
this.state = 2;
if(callback) callback(this.polygon);
}
getPositions() {
return this.positions;
}
getLnglats() {
return Cesium.caratesianArrToLnglatArr(this.positions);
}
getAttr() {
if (!this.polygon) return;
var obj = {};
var polygon = this.polygon.polygon;
obj.fill = polygon.fill._value;
obj.outline = polygon.outline._value;
obj.outlineWidth = polygon.outlineWidth._value;
obj.outlineColor = polygon.outlineColor._value;
obj.clampToGround = line.clampToGround._value;
obj.color = line.material.color._value;
return obj;
}
setStyle() {}
remove() {
if (this.polygon) {
this.state = 0;
this.viewer.entities.remove(this.polygon);
this.polygon = null;
}
}
setVisible(vis) {
this.polygon.show = vis;
}
forbidDrawWorld(isForbid) {
this.viewer.scene.screenSpaceCameraController.enableRotate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTilt = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTranslate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableInputs = !isForbid;
}
destroy() {
if (this.handler) {
this.handler.destroy();
this.handler = null;
}
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
}
if (this.polygon) {
this.viewer.entities.remove(this.polygon);
this.polygon = null;
}
if (this.polyline) {
this.viewer.entities.remove(this.polyline);
this.polyline = null;
}
this.positions = [];
this.style = null;
if (this.modifyPoint) {
this.viewer.entities.remove(this.modifyPoint);
this.modifyPoint = null;
}
for (var i = 0; i < this.gonPointArr.length; i++) {
var point = this.gonPointArr[i];
this.viewer.entities.remove(point);
}
this.gonPointArr = [];
this.state = 0;
if (this.prompt) this.prompt.destroy();
if (this.polyline) {
this.polyline = null;
this.viewer.entities.remove(this.polyline);
}
}
getCatesian3FromPX(px, viewer, entitys) {
var picks = viewer.scene.drillPick(px);
this.viewer.scene.render();
var cartesian;
var isOn3dtiles = false;
for (var i = 0; i < picks.length; i++) {
var isContinue = false;
for (var step = 0; step < entitys.length; step++) {
if (entitys[step] && picks[i].id && entitys[step].objId == picks[i].id.objId) {
isContinue = true;
break;
}
}
if (isContinue) continue;
if ((picks[i] && picks[i].primitive) || picks[i] instanceof Cesium.Cesium3DTileFeature) { //模型上拾取
isOn3dtiles = true;
}
}
if (isOn3dtiles) {
cartesian = viewer.scene.pickPosition(px);
} else {
var ray = viewer.camera.getPickRay(px);
if (!ray) return null;
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
}
return cartesian;
}
}
\ No newline at end of file
import movePrompt from './MovePrompt';
import addCompany from './addCompany';
export default class CreatePolyline {
constructor(viewer, style) {
this.objId = Number((new Date()).getTime() + "" + Number(Math.random() * 1000).toFixed(0));
this.viewer = viewer;
this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.polyline = null;
this.positions = [];
this.style = style;
this.floatPoint = null;
this.linePointArr = [];
this.modifyPoint = null;
this.state = 0;
//初始化鼠标提示框
this.prompt = new movePrompt.movePrompt(viewer);
// this.prompt.updatePrompt({x: 495, y: 407}, "单击开始绘制");
}
start(callBack) {
var that = this;
this.handler.setInputAction(function (evt) { //单机开始绘制
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.polyline]);
if (that.positions.length == 0) {
that.positions.push(cartesian.clone());
that.floatPoint = addCompany.createPoint.call(that, {position: cartesian.clone()});
}
that.positions.push(cartesian);
var point = addCompany.createPoint.call(that, {position: cartesian});
point.wz = that.linePointArr.length;
that.linePointArr.push(point);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
this.handler.setInputAction(function (evt) { //移动时绘制线
if (that.positions.length < 1) {
that.prompt.updatePrompt(evt.endPosition, "单击开始绘制");
return;
}
that.prompt.updatePrompt(evt.endPosition, "单击新增,右键结束");
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.polyline]);
if (that.floatPoint) that.floatPoint.position.setValue(cartesian);
if (that.positions.length == 2) {
if (!Cesium.defined(that.polyline)) {
that.polyline = addCompany.createPolyline.call(that);
}
}
if (that.polyline) {
that.positions.pop();
that.positions.push(cartesian);
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.handler.setInputAction(function (evt) { //单机开始绘制
if (!that.polyline) {
return;
}
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.polyline]);
that.state = 1;
that.handler.destroy();
if (that.floatPoint) {
if (that.floatPoint) that.floatPoint.show = false;
that.floatPoint = null;
}
that.positions.pop();
that.positions.push(cartesian);
var point = addCompany.createPoint.call(that, {position: cartesian});
point.wz = that.linePointArr.length;
that.linePointArr.push(point);
if(that.prompt){
that.prompt.destroy();
that.prompt = null;
}
if (callBack) callBack(that.polyline);
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}
startModify(callback) {
if (this.state != 2 && this.state != 1) return; //表示还没绘制完成
if (!this.modifyHandler) this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
var that = this;
for (var i = 0; i < that.linePointArr.length; i++) {
var point = that.linePointArr[i];
if (point) point.show = true;
}
this.modifyHandler.setInputAction(function (evt) {
if(!that.polyline) return ;
var pick = that.viewer.scene.pick(evt.position);
if (Cesium.defined(pick) && pick.id) {
if (!pick.id.objId)
that.modifyPoint = pick.id;
that.forbidDrawWorld(true);
} else {
for (var i = 0; i < that.linePointArr.length; i++) {
var point = that.linePointArr[i];
if (point) point.show = false;
}
if (that.modifyHandler) {
that.modifyHandler.destroy();
that.modifyHandler = null;
if(callback) callback(that.polyline);
}
that.state = 2;
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
this.modifyHandler.setInputAction(function (evt) {
if (that.positions.length < 1 || !that.modifyPoint) return;
//that.modifyPrompt.updatePrompt(evt.endPosition, "鼠标拖动修改");
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.polyline, that.modifyPoint]);
if (cartesian) {
that.modifyPoint.position.setValue(cartesian);
that.positions[that.modifyPoint.wz] = cartesian;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.modifyHandler.setInputAction(function (evt) {
if (!that.modifyPoint) return;
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.polyline, that.modifyPoint]);
that.modifyPoint.position.setValue(cartesian);
that.positions[that.modifyPoint.wz] = cartesian;
that.modifyPoint = null;
that.forbidDrawWorld(false);
}, Cesium.ScreenSpaceEventType.LEFT_UP);
}
endModify(callback) {
for (var i = 0; i < this.linePointArr.length; i++) {
var point = this.linePointArr[i];
if (point) point.show = false;
}
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
if(callback) callback(this.polyline);
}
this.state = 2;
}
getPositions() {
return this.positions;
}
getAttr() {
if (!this.polyline) return;
var obj = {};
var line = this.polyline.polyline;
obj.width = line.width._value;
obj.clampToGround = line.clampToGround._value;
obj.color = line.material.color._value;
obj.lineType = "solid";
return obj;
}
setStyle(obj) {}
remove() {
if (this.polyline) {
this.state = 0;
this.viewer.entities.remove(this.polyline);
this.polyline = null;
}
}
setVisible(vis) {
this.polyline.show = vis;
}
forbidDrawWorld(isForbid) {
this.viewer.scene.screenSpaceCameraController.enableRotate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTilt = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTranslate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableInputs = !isForbid;
}
destroy() {
this.linePointArr = [];
if (this.handler) {
this.handler.destroy();
this.handler = null;
}
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
}
if (this.polyline) {
this.viewer.entities.remove(this.polyline);
this.polyline = null;
}
if (this.floatPoint) {
this.viewer.entities.remove(this.floatPoint);
this.floatPoint = null;
}
this.positions = [];
this.style = null;
for (var i = 0; i < this.linePointArr.length; i++) {
var point = this.linePointArr[i];
this.viewer.entities.remove(point);
}
this.linePointArr = [];
this.modifyPoint = null;
if (this.prompt) this.prompt.destroy();
//if (this.modifyPrompt) this.modifyPrompt.destroy();
}
getCatesian3FromPX(px, viewer, entitys) {
var picks = viewer.scene.drillPick(px);
this.viewer.scene.render();
var cartesian;
var isOn3dtiles = false;
for (var i = 0; i < picks.length; i++) {
var isContinue = false;
for (var step = 0; step < entitys.length; step++) {
if (entitys[step] && picks[i].id && entitys[step].objId == picks[i].id.objId) {
isContinue = true;
break;
}
}
if (isContinue) continue;
if ((picks[i] && picks[i].primitive) || picks[i] instanceof Cesium.Cesium3DTileFeature) { //模型上拾取
isOn3dtiles = true;
}
}
if (isOn3dtiles) {
cartesian = viewer.scene.pickPosition(px);
} else {
var ray = viewer.camera.getPickRay(px);
if (!ray) return null;
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
}
return cartesian;
}
}
\ No newline at end of file
import movePrompt from './MovePrompt';
import addCompany from './addCompany';
export default class CreateRectangle {
constructor(viewer, style) {
this.objId = Number((new Date()).getTime() + "" + Number(Math.random() * 1000).toFixed(0));
this.viewer = viewer;
this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.rectangle = null;
this.screenSpaceCameraController = [];
this.style = style;
this.rightdownPoint = null;
this.leftupPoint = null;
this.leftup = null;
this.rightdown = null;
this.radius = 0;
this.modifyPoint = null;
this.state = 0;
this.pointArr = [];
this.prompt = new movePrompt.movePrompt(viewer);
}
start(callBack) {
var that = this;
this.handler.setInputAction(function (evt) { //单机开始绘制
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, []);
if (!that.leftupPoint) {
that.leftup = cartesian;
that.leftupPoint = addCompany.createPoint.call(that, {position: cartesian});
that.leftupPoint.typeAttr = "leftup";
that.rightdownPoint = addCompany.createPoint.call(that, {position: cartesian.clone()});
that.rightdown = cartesian.clone();
that.rightdownPoint.typeAttr = "rightdown";
that.rectangle = addCompany.createRectangle.call(that);
} else {
return;
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
this.handler.setInputAction(function (evt) { //移动时绘制线
if (!that.leftupPoint) {
that.prompt.updatePrompt(evt.endPosition, "单击开始绘制");
return;
}
that.prompt.updatePrompt(evt.endPosition, "右键结束");
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, []);
if (that.rightdownPoint) {
that.rightdownPoint.position.setValue(cartesian);
that.rightdown = cartesian.clone();
}
that.radius = Cesium.Cartesian3.distance(cartesian, that.leftup);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.handler.setInputAction(function () { //单机开始绘制
if (!that.rectangle) {
return;
}
that.state = 1;
that.handler.destroy();
if (that.rightdownPoint) that.rightdownPoint.show = false;
if (that.leftupPoint) that.leftupPoint.show = false;
if (that.prompt) {
that.prompt.destroy();
that.prompt = null;
}
if (callBack) callBack(that.rectangle);
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}
startModify(callback) {
if (this.state != 2 && this.state != 1) return; //表示还没绘制完成
if (!this.modifyHandler) this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
var that = this;
if (that.rightdownPoint) that.rightdownPoint.show = true;
if (that.leftupPoint) that.leftupPoint.show = true;
this.modifyHandler.setInputAction(function (evt) {
if (!that.rectangle) return;
var pick = that.viewer.scene.pick(evt.position);
if (Cesium.defined(pick) && pick.id) {
if (!pick.id.objId)
that.modifyPoint = pick.id;
that.forbidDrawWorld(true);
} else {
if (that.rightdownPoint) that.rightdownPoint.show = false;
if (that.leftupPoint) that.leftupPoint.show = false;
if (that.modifyHandler) {
that.modifyHandler.destroy();
that.modifyHandler = null;
if (callback) callback(that.rectangle);
}
that.state = 2;
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
this.modifyHandler.setInputAction(function (evt) {
if (!that.modifyPoint) return;
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.rectangle, that.modifyPoint]);
if (!cartesian) {
return;
}
if (that.modifyPoint.typeAttr == "leftup") {
that.leftup = cartesian
that.leftupPoint.position.setValue(that.leftup);
that.rectangle.position.setValue(that.leftup);
} else {
that.rightdown = cartesian
that.rightdownPoint.position.setValue(that.rightdown);
}
that.radius = Cesium.Cartesian3.distance(that.rightdown, that.leftup);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.modifyHandler.setInputAction(function (evt) {
if (!that.modifyPoint) return;
that.modifyPoint = null;
that.forbidDrawWorld(false);
}, Cesium.ScreenSpaceEventType.LEFT_UP);
}
endModify(callback) {
if (this.rightdownPoint) this.rightdownPoint.show = false;
if (this.leftupPoint) this.leftupPoint.show = false;
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
if (callback) callback(this.rectangle);
}
this.state = 2;
}
getPositions() {
return [this.leftup, this.rightdown]
}
getAttr() {
return obj;
}
setStyle(obj) {}
remove() {
if (this.rectangle) {
this.state = 0;
this.viewer.entities.remove(this.rectangle);
this.rectangle = null;
}
}
setVisible(vis) {
this.rectangle.show = vis;
}
forbidDrawWorld(isForbid) {
this.viewer.scene.screenSpaceCameraController.enableRotate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTilt = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTranslate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableInputs = !isForbid;
}
destroy() {
if (this.handler) {
this.handler.destroy();
this.handler = null;
}
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
}
if (this.rectangle) {
this.viewer.entities.remove(this.rectangle);
this.rectangle = null;
}
if (this.rightdownPoint) {
this.viewer.entities.remove(this.rightdownPoint);
this.rightdownPoint = null;
}
if (this.leftupPoint) {
this.viewer.entities.remove(this.leftupPoint);
this.leftupPoint = null;
}
this.style = null;
this.modifyPoint = null;
if (this.prompt) this.prompt.destroy();
}
getCatesian3FromPX(px, viewer, entitys) {
var picks = viewer.scene.drillPick(px);
this.viewer.scene.render();
var cartesian;
var isOn3dtiles = false;
for (var i = 0; i < picks.length; i++) {
var isContinue = false;
for (var step = 0; step < entitys.length; step++) {
if (entitys[step] && picks[i].id && entitys[step].objId == picks[i].id.objId) {
isContinue = true;
break;
}
}
if (isContinue) continue;
if ((picks[i] && picks[i].primitive) || picks[i] instanceof Cesium.Cesium3DTileFeature) { //模型上拾取
isOn3dtiles = true;
}
}
if (isOn3dtiles) {
cartesian = viewer.scene.pickPosition(px);
} else {
var ray = viewer.camera.getPickRay(px);
if (!ray) return null;
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
}
return cartesian;
}
}
\ No newline at end of file
import createBillboard from './createBillboard';
import createPolyline from './createPolyline';
import createPolygon from './createPolygon';
import createCircle from './createCircle';
import createRectangle from './createRectangle';
export default {
drawTool: class DrawTool {
constructor(obj) {
if (!obj.viewer || !obj) {
console.warn("缺少必要参数!--viewer");
return;
}
this.viewer = obj.viewer;
this.hasEdit = obj.hasEdit;
this.toolArr = [];
this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.show = obj.drawEndShow;
// this.lastSelectEntity = null;
}
startDraw(opt) {
var that = this;
if (opt.type == "polyline") {
var polyline = new createPolyline(this.viewer, opt.style);
polyline.start(function (evt) {
if (that.hasEdit) {
that.unbindEdit();
polyline.startModify(opt.modifySuccess);
that.lastSelectEntity = polyline;
}
if (opt.success) opt.success(evt);
if (that.show == false) polyline.setVisible(false);
});
this.toolArr.push(polyline);
}
if (opt.type == "polygon") {
var polygon = new createPolygon(this.viewer, opt.style);
polygon.start(function () {
if (that.hasEdit) {
that.unbindEdit();
polygon.startModify();
that.lastSelectEntity = polygon;
}
if (opt.success) opt.success(polygon);
if (that.show == false) polygon.setVisible(false);
});
this.toolArr.push(polygon);
}
if (opt.type == "billboard") {
var billboard = new createBillboard(this.viewer, opt.style);
billboard.start(function () {
if (that.hasEdit) {
that.unbindEdit();
billboard.startModify();
that.lastSelectEntity = billboard;
}
if (opt.success) opt.success(billboard);
if (that.show == false) billboard.setVisible(false);
});
this.toolArr.push(billboard);
}
if (opt.type == "circle") {
var circle = new createCircle(this.viewer, opt.style);
circle.start(function () {
if (that.hasEdit) {
that.unbindEdit();
circle.startModify();
that.lastSelectEntity = circle;
}
if (opt.success) opt.success(circle);
if (that.show == false) circle.setVisible(false);
});
this.toolArr.push(circle);
}
if (opt.type == "rectangle") {
var rectangle = new createRectangle(this.viewer, opt.style);
rectangle.start(function () {
if (that.hasEdit) {
that.unbindEdit();
rectangle.startModify();
that.lastSelectEntity = rectangle;
}
if (opt.success) opt.success(rectangle);
if (that.show == false) rectangle.setVisible(false);
});
this.toolArr.push(rectangle);
}
//重写材质
if (opt.type == "flowPolyline") {
var polyline = new CreatePolyline(this.viewer, opt.style);
polyline.start(function () {
if (that.hasEdit) {
that.unbindEdit();
polyline.startModify();
}
if (opt.success) opt.success(polyline);
});
this.toolArr.push(polyline);
}
}
createByPositions(opt) {
if (this.hasEdit) {
this.bindEdit();
}
if (!opt) opt = {};
if (opt.type == "polyline") {
var polyline = new CreatePolyline(this.viewer, opt.style);
polyline.createByPositions(opt.positions, opt.success);
this.toolArr.push(polyline);
}
if (opt.type == "polygon") {
var polygon = new CreatePolygon(this.viewer, opt.style);
polygon.createByPositions(opt.positions, opt.success);
this.toolArr.push(polygon);
}
if (opt.type == "billboard") {
var billboard = new CreateBillboard(this.viewer, opt.style);
billboard.createByPositions(opt.positions, function(){
if(opt.success) opt.success(billboard)
});
this.toolArr.push(billboard);
}
}
destroy() {
for (var i = 0; i < this.toolArr.length; i++) {
var obj = this.toolArr[i];
obj.destroy();
}
}
bindEdit() {
var that = this;
this.handler.setInputAction(function (evt) { //单机开始绘制
var pick = that.viewer.scene.pick(evt.position);
if (Cesium.defined(pick) && pick.id) {
for (var i = 0; i < that.toolArr.length; i++) {
if (pick.id.objId == that.toolArr[i].objId && (that.toolArr[i].state == 1||that.toolArr[i].state == 2)) {
if (that.lastSelectEntity) {
that.lastSelectEntity.endModify();
}
that.toolArr[i].startModify();
that.lastSelectEntity = that.toolArr[i];
break;
}
}
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}
unbindEdit() {
for (var i = 0; i < this.toolArr.length; i++) {
this.toolArr[i].endModify();
}
}
getCatesian3FromPX(px, viewer, entitys) {
var picks = viewer.scene.drillPick(px);
this.viewer.scene.render();
var cartesian;
var isOn3dtiles = false;
for (var i = 0; i < picks.length; i++) {
var isContinue = false;
for (var step = 0; step < entitys.length; step++) {
if (entitys[step] && picks[i].id && entitys[step].objId == picks[i].id.objId) {
isContinue = true;
break;
}
}
if (isContinue) continue;
if ((picks[i] && picks[i].primitive) || picks[i] instanceof Cesium.Cesium3DTileFeature) { //模型上拾取
isOn3dtiles = true;
}
}
if (isOn3dtiles) {
cartesian = viewer.scene.pickPosition(px);
} else {
var ray = viewer.camera.getPickRay(px);
if (!ray) return null;
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
}
return cartesian;
}
}
}
\ No newline at end of file
export default
class Roller {
constructor(obj) {
this.imageryLayers = obj.imageryLayers;
this.imageryLayers.addImageryProvider(new Cesium.WebMapTileServiceImageryProvider({
url: obj.url,
layer: "tdtBasicLayer",
style: "default",
format: "image/jpeg",
tileMatrixSetID: "GoogleMapsCompatible",
show: false
})).splitDirection = Cesium.ImagerySplitDirection.LEFT;
}
screen(opt) {
var slider = opt.slider, handler = new Cesium.ScreenSpaceEventHandler(slider), moveActive = false;
function move(movement) {
if (!moveActive) return;
var relativeOffset = movement.endPosition.x;
var splitPosition =
(slider.offsetLeft + relativeOffset) /
slider.parentElement.offsetWidth;
slider.style.left = 100.0 * splitPosition + "%";
opt.viewer.scene.imagerySplitPosition = splitPosition;
}
handler.setInputAction(function () {
moveActive = true;
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
handler.setInputAction(function () {
moveActive = true;
}, Cesium.ScreenSpaceEventType.PINCH_START);
handler.setInputAction(move, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
handler.setInputAction(move, Cesium.ScreenSpaceEventType.PINCH_MOVE);
handler.setInputAction(function () {
moveActive = false;
}, Cesium.ScreenSpaceEventType.LEFT_UP);
handler.setInputAction(function () {
moveActive = false;
}, Cesium.ScreenSpaceEventType.PINCH_END);
}
}
\ No newline at end of file