cesium 计算两点之间的距离

//positions 包含两个点的数组
disTance=function(positions){
			var distance = 0;
			for (var i = 0; i < positions.length - 1; i++) { 
				
			var point1cartographic = Cesium.Cartographic.fromCartesian(positions[i]);
			var point2cartographic = Cesium.Cartographic.fromCartesian(positions[i+1]);
			/**根据经纬度计算出距离**/
			var geodesic = new Cesium.EllipsoidGeodesic();
			geodesic.setEndPoints(point1cartographic, point2cartographic);
			var s = geodesic.surfaceDistance;
		    //返回两点之间的距离
//			s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));	
			s=Math.abs(point2cartographic.height - point1cartographic.height);	
			distance = distance + s;
			}	
			return distance.toFixed(3);
		}	

绘制两个点,把平面坐标转换为经纬度坐标:

var cartesian = viewer.scene.pickPosition(e.position);
result.points.push(cartesian);        
 var points=[];
        		result.points.map((item)=>{
			    var cartographic = Cesium.Cartographic.fromCartesian(item);
			    var point={};
			    point.x = Cesium.Math.toDegrees(cartographic.longitude);
			    point.y= Cesium.Math.toDegrees(cartographic.latitude);
			    point.z = cartographic.height;
     	        points.push(point);
			});

 

你可能感兴趣的:(js)