Cesium之【三角】量测

标签: Cesium  量算  JavaScript

//****************************三角测量 ************************************************//
var measureTriangle = function (viewer, handler){	
	handler_g = handler = new Cesium.ScreenSpaceEventHandler(viewer.scene._imageryLayerCollection);
	var positionsTriangle = [];
	var tempPoints ;//保存直角点
	var poly = null;
	var tooltip = document.getElementById("toolTip");
	var cartesian = null;
	var floatingPoint;//浮动点
	tooltip.style.display = "block";
	
	handler.setInputAction(function(movement){
		tooltip.style.left = movement.endPosition.x + 3 + "px";
		tooltip.style.top = movement.endPosition.y - 25 + "px";
        tooltip.innerHTML ='<p>单击开始,双击结束</p>';
		cartesian = viewer.scene.pickPosition(movement.endPosition); 
		//cartesian = viewer.scene.camera.pickEllipsoid(movement.endPosition, viewer.scene.globe.ellipsoid);
		if(positionsTriangle.length >= 2){
			if (!Cesium.defined(poly)) {				
				poly = new PolylinePrimitive(positionsTriangle);
			}else{
				positionsTriangle.pop();				
										
				positionsTriangle.push(cartesian.clone());
				
				tempPoints= point_conf(positionsTriangle);	
				
			}
			// tooltip.innerHTML='<p>'+distance+'米</p>';
		}
	},Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	
	handler.setInputAction(function(movement){
		tooltip.style.display = "none";
		
		// cartesian = viewer.scene.camera.pickEllipsoid(movement.position, viewer.scene.globe.ellipsoid);
		if(positionsTriangle.length == 0) {
			
			cartesian = viewer.scene.pickPosition(movement.position); 
			
			positionsTriangle.push(cartesian.clone());
			
			positionsTriangle.push(cartesian.clone());
			
			
			// tempPoints= point_conf(positionsTriangle);	

			floatingPoint_g = floatingPoint = viewer.entities.add({
				parent:measure_entities,
				name : '多边形面积',
				position : positionsTriangle[0],			
				point : {
					pixelSize : 5,
					color : Cesium.Color.RED,
					outlineColor : Cesium.Color.WHITE,
					outlineWidth : 2,
					heightReference:Cesium.HeightReference.none 
				}
			});	
		}		
	},Cesium.ScreenSpaceEventType.LEFT_CLICK);
	 
	handler.setInputAction(function(movement){
		handler.destroy();	
		// handler_g.destroy();//关闭事件句柄		
		tooltip.style.display = "none";
		
		// tempPoints =point_conf(positionsTriangle);
		//在三维场景中添加线
		var tempPositions1 =[];
		var tempPositions2 =[];
		tempPositions1.push(positionsTriangle[0].clone());
		tempPositions1.push(tempPoints.clone());
		var textDistance = getHeight(tempPositions1) + "米";
		
		viewer.entities.add({
			parent:measure_entities,
			name : '等经纬度',
			position : tempPositions1[0].clone(),
			polyline : {					
				show : true,
				positions : tempPositions1,
				material :  new Cesium.PolylineDashMaterialProperty({
						color: Cesium.Color.RED
					}),
				width : 2						
			},
			label : {
				text : textDistance,
				font : '18px sans-serif',
				fillColor : Cesium.Color.GOLD,
				style: Cesium.LabelStyle.FILL_AND_OUTLINE,
				outlineWidth : 2,
				verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
				pixelOffset : new Cesium.Cartesian2(20, -20)
			}
		});		
		
		tempPositions2.push(tempPoints.clone());
		tempPositions2.push(positionsTriangle[1].clone());
		textDistance = getDistance(tempPositions2) + "公里";
		
		viewer.entities.add({
			parent:measure_entities,
			name : '等高度直线',
			position : tempPositions2[0].clone(),
			polyline : {					
				show : true,
				positions : tempPositions2,
				material :  new Cesium.PolylineDashMaterialProperty({
						color: Cesium.Color.RED
					}),
				width : 2						
			},
			label : {
				text : textDistance,
				font : '18px sans-serif',
				fillColor : Cesium.Color.GOLD,
				style: Cesium.LabelStyle.FILL_AND_OUTLINE,
				outlineWidth : 2,
				verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
				pixelOffset : new Cesium.Cartesian2(60, -20)
			}
		});		

    }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK );	

	
	var PolylinePrimitive = (function(){
		function _(positions){
			this.options = {
				parent:measure_entities,
				name:'直线',
				polyline : {					
					show : true,
					positions : [],
					material : Cesium.Color.GOLD ,
					width : 2						
				},
				label : {					
					font : '18px sans-serif',
					fillColor : Cesium.Color.GOLD,
					style: Cesium.LabelStyle.FILL_AND_OUTLINE,
					outlineWidth : 2,
					verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
					pixelOffset : new Cesium.Cartesian2(20, -40)
				}
			};
			
			this.positions = positions;
			this._init();
		}
	
		_.prototype._init = function(){
			var _self = this;
			var _update = function(){				
				return _self.positions;
			};
			var _update_label = function(){
				return _self.positions[1].clone();
			};
			var _text = function(){
				var text_temp = getSpaceDistance(_self.positions);
				text_temp = text_temp +"公里";
				return text_temp;
			};
			//实时更新polygon.hierarchy
			this.options.polyline.positions = new Cesium.CallbackProperty(_update,false);
			this.options.position = new Cesium.CallbackProperty(_update_label,false);
			this.options.label.text =new Cesium.CallbackProperty(_text,false);
			viewer.entities.add(this.options);
		};	
		return _;
	})();
	
};

 

 

原文链接:加载失败,请重新获取