//from cdxiaodao cesium.js //measure or survey point, height, distance, area (function (window) { 'use strict'; function define_CesiumSurvey() { //Main object var CesiumSurvey = {}; var handler; //Get 3D position String when left_click CesiumSurvey.getPosition = function (viewer) { handler = handler && handler.destroy(); handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); var resultStr; handler.setInputAction(function (movement) { var cp = viewer.scene.pickPosition(movement.position); var pickedPositionString = cp.toString(); //获取地理坐标(经度、纬度、高程) var cartographic = Cesium.Cartographic.fromCartesian(cp); var longitudeString = Cesium.Math.toDegrees(cartographic.longitude); var latitudeString = Cesium.Math.toDegrees(cartographic.latitude); var heightString = cartographic.height; var getResult = pickedPositionString + '\r\n' + '======================' + '\r\n' + '经度:' + longitudeString + '\r\n' + '纬度:' + latitudeString + '\r\n' + '高程:' + heightString + '\r\n' + '======================' + '\r\n'; resultStr = longitudeString + ',' + latitudeString + ',' + heightString; alert(getResult); }, Cesium.ScreenSpaceEventType.LEFT_CLICK); handler.setInputAction(function () { handler = handler && handler.destroy(); }, Cesium.ScreenSpaceEventType.RIGHT_CLICK); return resultStr; }; //Get height String from 3dtiles to Mouse's position CesiumSurvey.measureHeight = function (viewer) { handler = handler && handler.destroy(); if (viewer.entities.getById('measureHeightLineEntity') !== undefined) { viewer.entities.removeById('measureHeightLineEntity'); } if (viewer.entities.getById('heightPlaneEntity') !== undefined) { viewer.entities.removeById('heightPlaneEntity'); } if (viewer.entities.getById('heightLabelEntity') !== undefined) { viewer.entities.removeById('heightLabelEntity'); } var isFirst = true; var firstPosition; var secondPosition; var previousPosition; var heightLineEntity = viewer.entities.add({ id: 'measureHeightLineEntity', polyline: { followSurface: false, width: 3, material: Cesium.Color.RED, show: true } }); var heightPlaneEntity = viewer.entities.add({ id: 'heightPlaneEntity', ellipse: { outline: true, show: false } }); var heightLabelEntity = viewer.entities.add({ id: 'heightLabelEntity', label: { show: false, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, horizontalOrigin: Cesium.HorizontalOrigin.LEFT, font: '16px Helvetica', fillColor: Cesium.Color.YELLOW, outlineColor: Cesium.Color.BLACK, outlineWidth: 1, style: Cesium.LabelStyle.FILL_AND_OUTLINE, showBackground: true, backgroundColor: new Cesium.Color(0, 0, 1, 0.8) } }); handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); handler.setInputAction(function (movement) { if (isFirst) { firstPosition = viewer.scene.pickPosition(movement.position); var cartographic = Cesium.Cartographic.fromCartesian(firstPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.position); firstPosition = viewer.scene.globe.pick(ray, viewer.scene); } isFirst = false; } else { secondPosition = viewer.scene.pickPosition(movement.position); var cartographic = Cesium.Cartographic.fromCartesian(secondPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.position); secondPosition = viewer.scene.globe.pick(ray, viewer.scene); } if (secondPosition) { previousPosition = secondPosition.clone(); } else { secondPosition = previousPosition.clone(); } var centerPosition = new Cesium.Cartesian3( (firstPosition.x * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition), (firstPosition.y * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition), (firstPosition.z * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition) ); heightLineEntity.polyline.positions = [firstPosition, centerPosition]; heightPlaneEntity.position = centerPosition; var cartographicCenter = Cesium.Cartographic.fromCartesian(centerPosition); var cartographicFirst = Cesium.Cartographic.fromCartesian(firstPosition); heightPlaneEntity.ellipse.height = cartographicCenter.height; heightPlaneEntity.ellipse.material = cartographicCenter.height > cartographicFirst.height > 0 ? Cesium.Color.YELLOW.withAlpha(0.5) : Cesium.Color.DEEPPINK.withAlpha(0.2); heightPlaneEntity.ellipse.semiMinorAxis = Cesium.Cartesian3.distance( centerPosition, secondPosition ); heightPlaneEntity.ellipse.semiMajorAxis = heightPlaneEntity.ellipse.semiMinorAxis; heightPlaneEntity.ellipse.show = true; heightLabelEntity.position = cartographicCenter.height > cartographicFirst.height > 0 ? centerPosition : firstPosition; heightLabelEntity.label.show = true; resultStr = cartographicCenter.height - cartographicFirst.height; heightLabelEntity.label.text = '高度:' + resultStr.toFixed(2) + '米'; var camera = viewer.scene.camera; heightLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.5, 0.0); handler = handler && handler.destroy(); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); var resultStr; handler.setInputAction(function (movement) { if (!isFirst) { secondPosition = viewer.scene.pickPosition(movement.endPosition); var cartographic = Cesium.Cartographic.fromCartesian(secondPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.endPosition); secondPosition = viewer.scene.globe.pick(ray, viewer.scene); } if (secondPosition) { previousPosition = secondPosition.clone(); } else { secondPosition = previousPosition.clone(); } var centerPosition = new Cesium.Cartesian3( (firstPosition.x * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition), (firstPosition.y * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition), (firstPosition.z * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition) ); heightLineEntity.polyline.positions = [firstPosition, centerPosition]; heightPlaneEntity.position = centerPosition; var cartographicCenter = Cesium.Cartographic.fromCartesian(centerPosition); var cartographicFirst = Cesium.Cartographic.fromCartesian(firstPosition); heightPlaneEntity.ellipse.height = cartographicCenter.height; heightPlaneEntity.ellipse.material = cartographicCenter.height > cartographicFirst.height > 0 ? Cesium.Color.YELLOW.withAlpha(0.5) : Cesium.Color.DEEPPINK.withAlpha(0.2); heightPlaneEntity.ellipse.semiMinorAxis = Cesium.Cartesian3.distance( centerPosition, secondPosition ); heightPlaneEntity.ellipse.semiMajorAxis = heightPlaneEntity.ellipse.semiMinorAxis; heightPlaneEntity.ellipse.show = true; heightLabelEntity.position = cartographicCenter.height > cartographicFirst.height > 0 ? centerPosition : firstPosition; heightLabelEntity.label.show = true; resultStr = cartographicCenter.height - cartographicFirst.height; heightLabelEntity.label.text = '高度:' + resultStr.toFixed(2) + '米'; var camera = viewer.scene.camera; heightLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.5, 0.0); } }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); handler.setInputAction(function () { handler = handler && handler.destroy(); }, Cesium.ScreenSpaceEventType.RIGHT_CLICK); return resultStr; }; //Get height String from 3dtiles to Mouse's position(三角尺) CesiumSurvey.measureHeightAngle = function (viewer) { handler = handler && handler.destroy(); if (viewer.entities.getById('measureHeightAngleLineEntity') !== undefined) { viewer.entities.removeById('measureHeightAngleLineEntity'); } if (viewer.entities.getById('heightAngleEntity') !== undefined) { viewer.entities.removeById('heightAngleEntity'); } if (viewer.entities.getById('heighthriEntity') !== undefined) { viewer.entities.removeById('heighthriEntity'); } if (viewer.entities.getById('heightLabelAngleEntity') !== undefined) { viewer.entities.removeById('heightLabelAngleEntity'); } var isFirst = true; var firstPosition; var secondPosition; var previousPosition; var heightLineEntity = viewer.entities.add({ id: 'measureHeightAngleLineEntity', polyline: { followSurface: false, width: 3, material: Cesium.Color.RED, show: true } }); var heightAngleEntity = viewer.entities.add({ id: 'heightAngleEntity', polyline: { width: 3, material: Cesium.Color.RED, show: true } }); var heighthriEntity = viewer.entities.add({ id: 'heighthriEntity', polyline: { width: 3, material: Cesium.Color.RED, show: true } }); var heightLabelEntity = viewer.entities.add({ id: 'heightLabelAngleEntity', label: { show: false, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, horizontalOrigin: Cesium.HorizontalOrigin.LEFT, font: '16px Helvetica', fillColor: Cesium.Color.YELLOW, outlineColor: Cesium.Color.BLACK, outlineWidth: 1, style: Cesium.LabelStyle.FILL_AND_OUTLINE, showBackground: true, backgroundColor: new Cesium.Color(0, 0, 1, 0.8) } }); handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); handler.setInputAction(function (movement) { if (isFirst) { firstPosition = viewer.scene.pickPosition(movement.position); var cartographic = Cesium.Cartographic.fromCartesian(firstPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.position); firstPosition = viewer.scene.globe.pick(ray, viewer.scene); } isFirst = false; } else { secondPosition = viewer.scene.pickPosition(movement.position); var cartographic = Cesium.Cartographic.fromCartesian(secondPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.position); secondPosition = viewer.scene.globe.pick(ray, viewer.scene); } if (secondPosition) { previousPosition = secondPosition.clone(); } else { secondPosition = previousPosition.clone(); } var centerPosition = new Cesium.Cartesian3( (firstPosition.x * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition), (firstPosition.y * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition), (firstPosition.z * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition) ); heightLineEntity.polyline.positions = [firstPosition, centerPosition]; heightAngleEntity.polyline.positions = [firstPosition, secondPosition]; heighthriEntity.polyline.positions = [centerPosition, secondPosition]; var cartographicCenter = Cesium.Cartographic.fromCartesian(centerPosition); var cartographicFirst = Cesium.Cartographic.fromCartesian(firstPosition); heightAngleEntity.polyline.material = cartographicCenter.height > cartographicFirst.height > 0 ? Cesium.Color.RED : Cesium.Color.YELLOW; heighthriEntity.polyline.material = cartographicCenter.height > cartographicFirst.height > 0 ? Cesium.Color.RED : Cesium.Color.YELLOW; heightLabelEntity.position = cartographicCenter.height > cartographicFirst.height > 0 ? centerPosition : firstPosition; heightLabelEntity.label.show = true; resultStr = cartographicCenter.height - cartographicFirst.height; var resulthoriStr = Cesium.Cartesian3.distance(centerPosition, secondPosition); var resultangleStr = Cesium.Cartesian3.distance(firstPosition, secondPosition); heightLabelEntity.label.text = '高度:' + resultStr.toFixed(2) + '米\n' + '水平:' + resulthoriStr.toFixed(2) + '米\n' + '直线:' + resultangleStr.toFixed(2) + '米'; var camera = viewer.scene.camera; heightLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.5, 0.0); handler = handler && handler.destroy(); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); var resultStr; handler.setInputAction(function (movement) { if (!isFirst) { secondPosition = viewer.scene.pickPosition(movement.endPosition); var cartographic = Cesium.Cartographic.fromCartesian(secondPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.endPosition); secondPosition = viewer.scene.globe.pick(ray, viewer.scene); } if (secondPosition) { previousPosition = secondPosition.clone(); } else { secondPosition = previousPosition.clone(); } var centerPosition = new Cesium.Cartesian3( (firstPosition.x * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition), (firstPosition.y * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition), (firstPosition.z * Cesium.Cartesian3.dot(firstPosition, secondPosition)) / Cesium.Cartesian3.magnitudeSquared(firstPosition) ); heightLineEntity.polyline.positions = [firstPosition, centerPosition]; heightAngleEntity.polyline.positions = [firstPosition, secondPosition]; heighthriEntity.polyline.positions = [centerPosition, secondPosition]; var cartographicCenter = Cesium.Cartographic.fromCartesian(centerPosition); var cartographicFirst = Cesium.Cartographic.fromCartesian(firstPosition); heightAngleEntity.polyline.material = cartographicCenter.height > cartographicFirst.height > 0 ? Cesium.Color.RED : Cesium.Color.YELLOW; heighthriEntity.polyline.material = cartographicCenter.height > cartographicFirst.height > 0 ? Cesium.Color.RED : Cesium.Color.YELLOW; heightLabelEntity.position = cartographicCenter.height > cartographicFirst.height > 0 ? centerPosition : firstPosition; heightLabelEntity.label.show = true; resultStr = cartographicCenter.height - cartographicFirst.height; var resulthoriStr = Cesium.Cartesian3.distance(centerPosition, secondPosition); var resultangleStr = Cesium.Cartesian3.distance(firstPosition, secondPosition); heightLabelEntity.label.text = '高度:' + resultStr.toFixed(2) + '米\n' + '水平:' + resulthoriStr.toFixed(2) + '米\n' + '直线:' + resultangleStr.toFixed(2) + '米'; var camera = viewer.scene.camera; heightLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.5, 0.0); } }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); handler.setInputAction(function () { handler = handler && handler.destroy(); }, Cesium.ScreenSpaceEventType.RIGHT_CLICK); return resultStr; }; //Get distence String between first left_click and second left_click CesiumSurvey.measureDist = function (viewer, type) { handler = handler && handler.destroy(); if (viewer.entities.getById('dynamicLineEntity') !== undefined) { viewer.entities.removeById('dynamicLineEntity'); } if (viewer.entities.getById('dynamicLineGroundEntity') !== undefined) { viewer.entities.removeById('dynamicLineGroundEntity'); } if (viewer.entities.getById('measureLineEntity') !== undefined) { viewer.entities.removeById('measureLineEntity'); } if (viewer.entities.getById('measureLineGroundEntity') !== undefined) { viewer.entities.removeById('measureLineGroundEntity'); } if (viewer.entities.getById('distLabelEntity') !== undefined) { viewer.entities.removeById('distLabelEntity'); } var isFirst = true; //是否为第一个点 var previousPosition; //前一个点的坐标 var currentPosition; //当前点坐标 var measureValue = 0.0; //随鼠标移动的标签entity var distLabelEntity = viewer.entities.add({ id: 'distLabelEntity', label: { show: false, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, horizontalOrigin: Cesium.HorizontalOrigin.LEFT, font: '16px Helvetica', fillColor: Cesium.Color.YELLOW, outlineColor: Cesium.Color.BLACK, outlineWidth: 1, style: Cesium.LabelStyle.FILL_AND_OUTLINE, showBackground: true, backgroundColor: new Cesium.Color(0, 0, 1, 0.8) } }); var show3d = true, show2d = false; if (type == 'two') { show2d = true; show3d = false; } //前一个点与鼠标连线的entity var dynamicLineEntity = viewer.entities.add({ id: 'dynamicLineEntity', polyline: { followSurface: false, width: 3, material: Cesium.Color.YELLOW, show: show3d } }); var dynamicLineGroundEntity = viewer.entities.add({ id: 'dynamicLineGroundEntity', polyline: { clampToGround: true, width: 3, material: Cesium.Color.YELLOW, show: show2d } }); //已输入的线段的entity var measureLineEntity = viewer.entities.add({ id: 'measureLineEntity', polyline: { followSurface: false, width: 3, material: Cesium.Color.RED, show: show3d } }); var measureLineGroundEntity = viewer.entities.add({ id: 'measureLineGroundEntity', polyline: { clampToGround: true, width: 3, material: Cesium.Color.RED, show: show2d } }); var measureLinePositonsArray = []; //存储已量测的线段的折点 handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); handler.setInputAction(function (movement) { if (isFirst) { previousPosition = viewer.scene.pickPosition(movement.position); var cartographic = Cesium.Cartographic.fromCartesian(previousPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.position); previousPosition = viewer.scene.globe.pick(ray, viewer.scene); } var tmp1 = previousPosition.clone(); measureValue = 0.0; isFirst = false; measureLinePositonsArray.push(tmp1); } else { currentPosition = viewer.scene.pickPosition(movement.position); var cartographic = Cesium.Cartographic.fromCartesian(currentPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.position); currentPosition = viewer.scene.globe.pick(ray, viewer.scene); } var tmp2 = currentPosition.clone(); measureLinePositonsArray.push(tmp2); measureLineEntity.polyline.positions = measureLinePositonsArray; measureLineGroundEntity.polyline.positions = measureLinePositonsArray; measureValue = measureValue + Cesium.Cartesian3.distance(currentPosition, previousPosition); previousPosition = currentPosition.clone(); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); var measureValueShow; handler.setInputAction(function (movement) { //move事件监听 if (!isFirst) { //console.log('move,不是第一个点'); //获取当前点的坐标 currentPosition = viewer.scene.pickPosition(movement.endPosition); var cartographic = Cesium.Cartographic.fromCartesian(currentPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.endPosition); currentPosition = viewer.scene.globe.pick(ray, viewer.scene); } if (currentPosition) { distLabelEntity.position = currentPosition; distLabelEntity.label.show = true; measureValueShow = measureValue + Cesium.Cartesian3.distance(currentPosition, previousPosition); distLabelEntity.label.text = '距离:' + measureValueShow.toFixed(2) + '米'; var camera = viewer.scene.camera; distLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 1, 0.0); dynamicLineEntity.polyline.positions = [previousPosition, currentPosition]; dynamicLineGroundEntity.polyline.positions = [previousPosition, currentPosition]; } } }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); handler.setInputAction(function () { //move事件监听 handler = handler && handler.destroy(); }, Cesium.ScreenSpaceEventType.RIGHT_CLICK); return measureValueShow; }; //Get area string by dynamic polygon on earth var pointArr = []; CesiumSurvey.measureArea = function (viewer) { var measureAreaValue = 0.0; var activeShapePoints = []; handler = handler && handler.destroy(); if (viewer.entities.getById('areaLabelEntity') !== undefined) { viewer.entities.removeById('areaLabelEntity'); } if (viewer.entities.getById('areaEntity') !== undefined) { viewer.entities.removeById('areaEntity'); } for (var i = 0; i < pointArr.length; i++) { viewer.entities.remove(pointArr[i]); } var areaLabelEntity = viewer.entities.add({ id: 'areaLabelEntity', label: { show: false, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, horizontalOrigin: Cesium.HorizontalOrigin.LEFT, font: '16px Helvetica', fillColor: Cesium.Color.YELLOW, outlineColor: Cesium.Color.BLACK, outlineWidth: 1, style: Cesium.LabelStyle.FILL_AND_OUTLINE, showBackground: true, backgroundColor: new Cesium.Color(0, 0, 1, 0.8) } }); function createPoint(worldPosition) { pointArr.push( viewer.entities.add({ position: worldPosition, point: { color: Cesium.Color.RED, pixelSize: 10, outlineWidth: 1, outlineColor: Cesium.Color.WHITE, disableDepthTestDistance: 10000 } }) ); } function drawPolygon() { if (viewer.entities.getById('areaEntity') !== undefined) { viewer.entities.removeById('areaEntity'); } viewer.entities.add({ id: 'areaEntity', polygon: { hierarchy: new Cesium.PolygonHierarchy(activeShapePoints), /*new Cesium.CallbackProperty(function () { return activeShapePoints; }, false),*/ material: new Cesium.ColorMaterialProperty(Cesium.Color.YELLOW.withAlpha(0.6)), classificationType: Cesium.ClassificationType.BOTH, outline: true, outlineColor: Cesium.Color.WHITE } }); } 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 radiansPerDegree = Cesium.Math.PI / 180.0; //角度转化为弧度(rad) var degreesPerRadian = 180.0 / Cesium.Math.PI; //弧度转化为角度 //计算多边形面积 function getArea(points) { var res = 0.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[0], points[j], points[k]); var dis_temp1 = distance(activeShapePoints[0], activeShapePoints[j]); var dis_temp2 = distance(activeShapePoints[0], activeShapePoints[k]); var tmp = 0.5 * dis_temp1 * dis_temp2 * Math.sin(totalAngle * radiansPerDegree); res = res + tmp; //console.log(res); } return Math.abs(res.toFixed(1)); //顺时针负,逆时针正 } /*角度*/ function Angle(p1, p2, p3) { var bearing12 = Bearing(p1, p2); var bearing13 = Bearing(p1, p3); var angle = bearing12 - bearing13; if (angle < 0) { angle += 360; } return angle; } /*方向*/ function Bearing(from, to) { var lat1 = from.lat * radiansPerDegree; var lon1 = from.lon * radiansPerDegree; var lat2 = to.lat * radiansPerDegree; var lon2 = to.lon * radiansPerDegree; 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 += Cesium.Math.PI * 2.0; } angle = angle * degreesPerRadian; //角度 return angle; } //选点 handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); handler.setInputAction(function (movement) { var earthPosition = viewer.scene.pickPosition(movement.position); //获取地理坐标(经度、纬度、高程) var cartographic = Cesium.Cartographic.fromCartesian(earthPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.position); earthPosition = viewer.scene.globe.pick(ray, viewer.scene); } if (Cesium.defined(earthPosition)) { activeShapePoints.push(earthPosition); if (activeShapePoints.length > 3) { drawPolygon(); } createPoint(earthPosition); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); //移动 handler.setInputAction(function (movement) { var newPosition = viewer.scene.pickPosition(movement.endPosition); var cartographic = Cesium.Cartographic.fromCartesian(newPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.endPosition); newPosition = viewer.scene.globe.pick(ray, viewer.scene); } activeShapePoints.pop(); activeShapePoints.push(newPosition); }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); //结束并计算 var areaResult; handler.setInputAction(function (movement) { handler = handler && handler.destroy(); if (activeShapePoints.length > 2) { var rightPosition = viewer.scene.pickPosition(movement.position); var cartographic = Cesium.Cartographic.fromCartesian(rightPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.position); rightPosition = viewer.scene.globe.pick(ray, viewer.scene); } var tempPoints = []; for (var pindex = 0; pindex < activeShapePoints.length; pindex++) { var cartographic = Cesium.Cartographic.fromCartesian(activeShapePoints[pindex].clone()); var longitudeString = Cesium.Math.toDegrees(cartographic.longitude); var latitudeString = Cesium.Math.toDegrees(cartographic.latitude); var heightString = cartographic.height; tempPoints.push({ lon: longitudeString, lat: latitudeString, hei: heightString }); } drawPolygon(); areaResult = getArea(tempPoints); areaLabelEntity.label.text = '面积:' + areaResult + '平方米'; areaLabelEntity.position = rightPosition.clone(); areaLabelEntity.label.show = true; var camera = viewer.scene.camera; areaLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.5, 0.0); } }, Cesium.ScreenSpaceEventType.RIGHT_CLICK); return areaResult; }; var measurePointArr = []; CesiumSurvey.measurePoint = function (viewer, img) { var poiResult; handler = handler && handler.destroy(); handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); handler.setInputAction(function (movement) { //获取鼠标点击处的坐标 var cartesian = viewer.scene.pickPosition(movement.position); var cartographic = Cesium.Cartographic.fromCartesian(cartesian); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.position); cartesian = viewer.scene.globe.pick(ray, viewer.scene); } cartographic = Cesium.Cartographic.fromCartesian(cartesian); height = cartographic.height; //地形高度 var lng = Cesium.Math.toDegrees(cartographic.longitude); var lat = Cesium.Math.toDegrees(cartographic.latitude); var heading = Cesium.Math.toDegrees(viewer.camera.heading); var pitch = Cesium.Math.toDegrees(viewer.camera.pitch); var roll = Cesium.Math.toDegrees(viewer.camera.roll); poiResult = lng.toFixed(4) + '\u00B0' + '\n' + lat.toFixed(4) + '\u00B0' + '\n' + height.toFixed(1) + '米'; console.log(lng + ',' + lat + ',' + height + ',' + heading + ',' + pitch + ',' + roll); var pinBuilder = new Cesium.PinBuilder(); //创建cesium pin标签 var point; if (img && img != '') { console.log(img); point = viewer.entities.add({ id: 'measurePointEntity' + new Date().getTime(), position: cartesian.clone(), billboard: { image: img, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, pixelOffset: new Cesium.Cartesian2(43.0, 0.0) // scale:0.3 }, label: { show: true, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, horizontalOrigin: Cesium.HorizontalOrigin.CENTER, font: 'bold 16px Helvetica', fillColor: Cesium.Color.WHITE, text: poiResult, pixelOffset: new Cesium.Cartesian2(56.0, -126.0), disableDepthTestDistance: 100000000 } }); } else { point = viewer.entities.add({ id: 'measurePointEntity' + new Date().getTime(), position: cartesian.clone(), billboard: { image: pinBuilder.fromText('测', Cesium.Color.RED, 36).toDataURL(), verticalOrigin: Cesium.VerticalOrigin.BOTTOM }, label: { show: true, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, horizontalOrigin: Cesium.HorizontalOrigin.CENTER, font: '16px sans-serif', fillColor: Cesium.Color.YELLOW, outlineColor: Cesium.Color.BLACK, outlineWidth: 1, style: Cesium.LabelStyle.FILL_AND_OUTLINE, text: poiResult, pixelOffset: new Cesium.Cartesian2(0.0, -14.0), pixelOffsetScaleByDistance: new Cesium.NearFarScalar(1.5e2, 3.0, 1.5e7, 0.5), backgroundColor: Cesium.Color.BLUE.withAlpha(0.8), showBackground: true } }); } measurePointArr.push(point); }, Cesium.ScreenSpaceEventType.LEFT_CLICK); handler.setInputAction(function (movement) { handler = handler && handler.destroy(); }, Cesium.ScreenSpaceEventType.RIGHT_CLICK); return poiResult; }; var mmhandler; CesiumSurvey.measureMovePoint = function (viewer, resultid) { mmhandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); //移动 mmhandler.setInputAction(function (movement) { var newPosition = viewer.scene.pickPosition(movement.endPosition); if (newPosition != undefined) { var cartographic = Cesium.Cartographic.fromCartesian(newPosition); var height = cartographic.height; //模型高度 if (Number(height) < 0) { var ray = viewer.camera.getPickRay(movement.endPosition); newPosition = viewer.scene.globe.pick(ray, viewer.scene); cartographic = Cesium.Cartographic.fromCartesian(newPosition); } height = cartographic.height; //地形高度 var lng = Cesium.Math.toDegrees(cartographic.longitude); var lat = Cesium.Math.toDegrees(cartographic.latitude); var camPoiR = viewer.camera.position; var camcartographic = Cesium.Cartographic.fromCartesian(camPoiR); var camheight = camcartographic.height; //地形高度 var camlng = Cesium.Math.toDegrees(camcartographic.longitude); var camlat = Cesium.Math.toDegrees(camcartographic.latitude); var heading = Cesium.Math.toDegrees(viewer.camera.heading); var pitch = Cesium.Math.toDegrees(viewer.camera.pitch); var roll = Cesium.Math.toDegrees(viewer.camera.roll); var poiResult = lng.toFixed(4) + '\u00B0' + ',' + lat.toFixed(4) + '\u00B0' + ',' + height.toFixed(1) + '米'; document.getElementById(resultid).innerHTML = poiResult; } }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); }; var cammmhandler; CesiumSurvey.cammeasureMovePoint = function (viewerC, camresultid) { cammmhandler = new Cesium.ScreenSpaceEventHandler(viewerC.scene.canvas); //移动 cammmhandler.setInputAction(function (movement) { var camPoiR = viewerC.camera.position; var camcartographic = Cesium.Cartographic.fromCartesian(camPoiR); var camheight = camcartographic.height; //地形高度 var camlng = Cesium.Math.toDegrees(camcartographic.longitude); var camlat = Cesium.Math.toDegrees(camcartographic.latitude); var heading = Cesium.Math.toDegrees(viewerC.camera.heading); var pitch = Cesium.Math.toDegrees(viewerC.camera.pitch); var roll = Cesium.Math.toDegrees(viewerC.camera.roll); var camResult = camlng.toFixed(4) + '\u00B0' + ',' + camlat.toFixed(4) + '\u00B0' + ',' + camheight.toFixed(1) + '米,' + heading.toFixed(2) + '\u00B0' + ',' + pitch.toFixed(2) + '\u00B0' + ',' + roll.toFixed(1) + '\u00B0'; document.getElementById(camresultid).innerHTML = camResult; }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); }; //Clear measure result CesiumSurvey.measureClear = function (viewer) { handler = handler && handler.destroy(); if (viewer.entities.getById('measureHeightLineEntity') !== undefined) { viewer.entities.removeById('measureHeightLineEntity'); } if (viewer.entities.getById('heightPlaneEntity') !== undefined) { viewer.entities.removeById('heightPlaneEntity'); } if (viewer.entities.getById('heightLabelEntity') !== undefined) { viewer.entities.removeById('heightLabelEntity'); } if (viewer.entities.getById('measureHeightAngleLineEntity') !== undefined) { viewer.entities.removeById('measureHeightAngleLineEntity'); } if (viewer.entities.getById('heightAngleEntity') !== undefined) { viewer.entities.removeById('heightAngleEntity'); } if (viewer.entities.getById('heighthriEntity') !== undefined) { viewer.entities.removeById('heighthriEntity'); } if (viewer.entities.getById('heightLabelAngleEntity') !== undefined) { viewer.entities.removeById('heightLabelAngleEntity'); } if (viewer.entities.getById('dynamicLineEntity') !== undefined) { viewer.entities.removeById('dynamicLineEntity'); } if (viewer.entities.getById('dynamicLineGroundEntity') !== undefined) { viewer.entities.removeById('dynamicLineGroundEntity'); } if (viewer.entities.getById('measureLineEntity') !== undefined) { viewer.entities.removeById('measureLineEntity'); } if (viewer.entities.getById('measureLineGroundEntity') !== undefined) { viewer.entities.removeById('measureLineGroundEntity'); } if (viewer.entities.getById('distLabelEntity') !== undefined) { viewer.entities.removeById('distLabelEntity'); } if (viewer.entities.getById('areaLabelEntity') !== undefined) { viewer.entities.removeById('areaLabelEntity'); } if (viewer.entities.getById('areaEntity') !== undefined) { viewer.entities.removeById('areaEntity'); } for (var i = 0; i < pointArr.length; i++) { viewer.entities.remove(pointArr[i]); } if (viewer.entities.getById('measurePointEntity') !== undefined) { viewer.entities.removeById('measurePointEntity'); } for (var i = 0; i < measurePointArr.length; i++) { viewer.entities.remove(measurePointArr[i]); } //measurePointArr = []; return true; }; //shutdown handler CesiumSurvey.clearHandler = function () { handler = handler && handler.destroy(); }; //shutdown mmhandler CesiumSurvey.clearMMHandler = function () { mmhandler = mmhandler && mmhandler.destroy(); }; //shutdown cammmhandler CesiumSurvey.clearCamMMHandler = function () { cammmhandler = cammmhandler && cammmhandler.destroy(); }; return CesiumSurvey; } if (typeof CesiumSurvey === 'undefined') { window.CesiumSurvey = define_CesiumSurvey(); } else { console.log('CesiumSurvey already defined.'); } })(window);