entity操作https://www.jianshu.com/p/8ae3624347cb
1. entity的操作:添加、隐藏、修改、去除、居中显示
Var rainEntity=viewer.entities.add({
id: "rain",
name: 'RainStation',
parent: rainLayer3D,
position: Cesium.Cartesian3.fromDegrees(lon, lat),
billboard: {
image: 'images/pointIcons/rain1.png',
scale:0.7,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM
},
label: {
text: rainfall,
font: '12px SimHei ',
Width: 3,
style: Cesium.LabelStyle.FILL,
fillColor: Cesium.Color.AQUA,
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
verticalOrigin: Cesium.VerticalOrigin.TOP
}
}); //添加
viewer.entities.getById("rain").show = false; //隐藏
viewer.entities.getById("rain").label.text= "drp"; //修改属性
viewer.entities.removeAll(); //移除所有
viewer.zoomTo(rainEntity); //居中显示
2. 去掉entity的双击事件
问题所在:双击entity,会放大视图,entity居中显示,且鼠标左键失去移动功能,鼠标滚轮失去作用
解决问题:
viewer.screenSpaceEventHandler.setInputAction(function(){},Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK );
获取当前视角高度
var scene = viewer.scene;
var ellipsoid = scene.globe.ellipsoid;
var height=ellipsoid.cartesianToCartographic(viewer.camera.position).height;
禁用事件
//大批量操作时,临时禁用事件可以提高性能
viewer.entities.suspendEvents();
//执行各种Entity操作
viewer.entities.resumeEvents();
3. 创建一个entity
var viewer = new Cesium.Viewer('cesiumContainer');
var wyoming = viewer.entities.add({
name : 'Wyoming',
polygon : {
hierarchy : Cesium.Cartesian3.fromDegreesArray([
-109.080842,45.002073,
-105.91517,45.002073,
-104.058488,44.996596,
-104.053011,43.002989,
-104.053011,41.003906,
-105.728954,40.998429,
-107.919731,41.003906,
-109.04798,40.998429,
-111.047063,40.998429,
-111.047063,42.000709,
-111.047063,44.476286,
-111.05254,45.002073]),
height : 0,
material : Cesium.Color.RED.withAlpha(0.5),
outline : true,
outlineColor : Cesium.Color.BLACK
}
});
viewer.zoomTo(wyoming);
4.Entity支持的所有的面和体
(1)折线 entity.polyline
折线 entity.polyline折线设置宽度
var entity = viewer.entities.add({
polyline : {
positions : Cesium.Cartesian3.fromDegreesArray([-77, 35,
-77.1, 35]),
width : 5,
material : Cesium.Color.RED
}});
viewer.zoomTo(viewer.entities);
var polyline = entity.polyline // For upcoming examples
折线可设置宽度
折线边线
polyline.material = new Cesium.PolylineOutlineMaterialProperty({
color : Cesium.Color.ORANGE,
outlineWidth : 3,
outlineColor : Cesium.Color.BLACK
});
折线边线
折线辉光
polyline.material = new Cesium.PolylineGlowMaterialProperty({
glowPower : 0.2,
color : Cesium.Color.BLUE
});
var redLine = viewer.entities.add({
name : '沿着地球表面的红线',
polyline : {
positions : Cesium.Cartesian3.fromDegreesArray(
[-75, 35,
-125, 35]),
width : 5,
material : Cesium.Color.RED
}
});
var glowingLine = viewer.entities.add({
name : '具有发光效果的线',
polyline : {
positions : Cesium.Cartesian3.fromDegreesArray(
[-75, 37, -125, 37]
),
width : 10,
material : new Cesium.PolylineGlowMaterialProperty({
glowPower : 0.2,
color : Cesium.Color.BLUE
})
}
});
var orangeOutlined = viewer.entities.add({
name : '具有一定高度的线',
polyline : {
positions : Cesium.Cartesian3.fromDegreesArrayHeights(
[-75, 39, 250000,-125, 39, 250000]
),
width : 5,
material : new Cesium.PolylineOutlineMaterialProperty({
color : Cesium.Color.ORANGE,
outlineWidth : 2,
outlineColor : Cesium.Color.BLACK
})
}
});
var yellowLine = viewer.entities.add({
name : '不贴着地表的线',
polyline : {
positions : Cesium.Cartesian3.fromDegreesArrayHeights(
[-75, 43, 500000,-125, 43, 500000]
),
width : 3,
followSurface : false, //是否贴着地表
material : Cesium.Color.PURPLE
}
});
(2) 多边形 entity.polygon
多边形 entity.polygonvar redPolygon = viewer.entities.add({
name : '贴着地表的多边形',
polygon : {
hierarchy : Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0,
-115.0, 32.0,
-107.0, 33.0,
-102.0, 31.0,
-102.0, 35.0]),
material : Cesium.Color.RED
}
});
var greenPolygon = viewer.entities.add({
name : '绿色拉伸多边形',
polygon : {
hierarchy : Cesium.Cartesian3.fromDegreesArray([-108.0, 42.0,
-100.0, 42.0,
-104.0, 40.0]),
extrudedHeight: 500000.0,
material : Cesium.Color.GREEN
}
});
var orangePolygon = viewer.entities.add({
name : '每个顶点具有不同拉伸高度的橘色多边形',
polygon : {
hierarchy : Cesium.Cartesian3.fromDegreesArrayHeights(
[-108.0, 25.0, 100000,
-100.0, 25.0, 100000,
-100.0, 30.0, 100000,
-108.0, 30.0, 300000]),
extrudedHeight: 0,
perPositionHeight : true,
material : Cesium.Color.ORANGE,
outline : true,
outlineColor : Cesium.Color.BLACK
}
});
var bluePolygon = viewer.entities.add({
name : '具有挖空效果的蓝色多边形',
polygon : {
hierarchy : {
positions : Cesium.Cartesian3.fromDegreesArray(
[-99.0, 30.0,
-85.0, 30.0,
-85.0, 40.0,
-99.0, 40.0]),
holes : [{
positions : Cesium.Cartesian3.fromDegreesArray([
-97.0, 31.0,
-97.0, 39.0,
-87.0, 39.0,
-87.0, 31.0
]),
holes : [{
positions : Cesium.Cartesian3.fromDegreesArray([
-95.0, 33.0,
-89.0, 33.0,
-89.0, 37.0,
-95.0, 37.0
]),
holes : [{
positions : Cesium.Cartesian3.fromDegreesArray([
-93.0, 34.0,
-91.0, 34.0,
-91.0, 36.0,
-93.0, 36.0
])
}]
}]
}]
},
material : Cesium.Color.BLUE,
outline : true,
outlineColor : Cesium.Color.BLACK
}
});
高度和垂直挤压设置
圆(circles)、椭圆(ellipses)、多边形(polygons)、矩形(rectangles)可以有一个高程属性 或者 垂直挤压变成体
wyoming.polygon.height = 200000;
wyoming.polygon.extrudedHeight = 250000;
11335939-43f79422edc13b7b.jpg
(3) 矩形 entity.rectangle
矩形 entity.rectangle/红色矩形
var redRectangle = viewer.entities.add({
name : 'Red translucent rectangle with outline',
rectangle : {
coordinates : Cesium.Rectangle.fromDegrees(-110.0, 20.0, -80.0, 25.0),
material : Cesium.Color.RED.withAlpha(0.5),
outline : true,
outlineColor : Cesium.Color.RED
}
});
//绿色旋转、拉伸的矩形
var greenRectangle = viewer.entities.add({
name : 'Green translucent, rotated, and extruded rectangle',
rectangle : {
coordinates : Cesium.Rectangle.fromDegrees(-100.0, 30.0, -90.0, 40.0),
material : Cesium.Color.GREEN.withAlpha(0.5),
rotation : Cesium.Math.toRadians(45),
extrudedHeight : 300000.0,
height : 100000.0,
outline : true,
outlineColor : Cesium.Color.GREEN
}
});
(4) 圆和椭圆entity.ellipse
圆和椭圆entity.ellipsear viewer = new Cesium.Viewer('cesiumContainer');
//浮空的绿色圆形
var greenCircle = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-111.0, 40.0, 150000.0),
name : 'Green circle at height',
ellipse : {
semiMinorAxis : 300000.0,
semiMajorAxis : 300000.0,
height: 200000.0, //浮空
material : Cesium.Color.GREEN
}
});
//红色椭圆形,位于地表,带轮廓
var redEllipse = viewer.entities.add({
//不带高度
position: Cesium.Cartesian3.fromDegrees(-103.0, 40.0),
name : 'Red ellipse on surface with outline',
ellipse : {
semiMinorAxis : 250000.0,
semiMajorAxis : 400000.0,
material : Cesium.Color.RED.withAlpha(0.5),
outline : true,
outlineColor : Cesium.Color.RED
}
});
//蓝色椭圆柱,旋转了角度
var blueEllipse = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 100000.0),
name : 'Blue translucent, rotated, and extruded ellipse',
ellipse : {
semiMinorAxis : 150000.0,
semiMajorAxis : 300000.0,
extrudedHeight : 200000.0, //拉伸
rotation : Cesium.Math.toRadians(45), //旋转
material : Cesium.Color.BLUE.withAlpha(0.7),
outline : true
}
});
viewer.zoomTo(viewer.entities);
(5) Corridor entity.corridor
Corridor entity.corridorar redCorridor = viewer.entities.add({
name : 'Red corridor on surface with rounded corners and outline',
corridor : {
positions : Cesium.Cartesian3.fromDegreesArray([
-100.0, 40.0,
-105.0, 40.0,
-105.0, 35.0
]),
width : 200000.0,
material : Cesium.Color.RED.withAlpha(0.5),
outline : true,
outlineColor : Cesium.Color.RED
}
});
var greenCorridor = viewer.entities.add({
name : 'Green corridor at height with mitered corners',
corridor : {
positions : Cesium.Cartesian3.fromDegreesArray(
[ -90.0, 40.0,
-95.0, 40.0,
-95.0, 35.0
]),
height: 100000.0,
width : 200000.0,
cornerType: Cesium.CornerType.MITERED,
material : Cesium.Color.GREEN
}
});
var blueCorridor = viewer.entities.add({
name : 'Blue extruded corridor with beveled corners and outline',
corridor : {
positions : Cesium.Cartesian3.fromDegreesArray(
[ 80.0, 40.0,
-85.0, 40.0,
-85.0, 35.0
]),
height : 200000.0,
extrudedHeight : 100000.0,
width : 200000.0,
cornerType: Cesium.CornerType.BEVELED,
material : Cesium.Color.BLUE.withAlpha(0.5),
outline : true,
outlineColor : Cesium.Color.BLUE
}
});
(6) 六面体盒子entity.box
六面体盒子entity.boxvar blueBox = viewer.entities.add({
name : 'Blue box',
//中心的位置
position: Cesium.Cartesian3.fromDegrees(-114.0, 40.0, 300000.0),
box : {
//长宽高
dimensions : new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
material : Cesium.Color.BLUE
}
});
var redBox = viewer.entities.add({
name : 'Red box with black outline',
position: Cesium.Cartesian3.fromDegrees(-107.0, 40.0, 300000.0),
box : {
dimensions : new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
material : Cesium.Color.RED,
outline : true, //显示轮廓
outlineColor : Cesium.Color.BLACK
}
});
var outlineOnly = viewer.entities.add({
name : 'Yellow box outline',
position: Cesium.Cartesian3.fromDegrees(-100.0, 40.0, 300000.0),
box : {
dimensions : new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
fill : false, //不显示填充
outline : true,
outlineColor : Cesium.Color.YELLOW
}
});
(7) 圆柱和圆锥 entity.cylinder
圆柱和圆锥 entity.cylindervar greenCylinder = viewer.entities.add({
name : 'Green cylinder with black outline',
position: Cesium.Cartesian3.fromDegrees(-100.0, 40.0, 200000.0),
cylinder : { //圆柱
length : 400000.0,
topRadius : 200000.0,
bottomRadius : 200000.0,
material : Cesium.Color.GREEN,
outline : true,
outlineColor : Cesium.Color.DARK_GREEN
}
});
var redCone = viewer.entities.add({
name : 'Red cone',
position: Cesium.Cartesian3.fromDegrees(-105.0, 40.0, 200000.0),
cylinder : {//圆锥
length : 400000.0,
topRadius : 0.0,
bottomRadius : 200000.0,
material : Cesium.Color.RED
}
});
(8) Polyline Volumes entity.polylineVolume
Polyline Volumes entity.polylineVolumevar viewer = new Cesium.Viewer('cesiumContainer');
function computeCircle(radius) {
var positions = [];
for (var i = 0; i < 360; i++) {
var radians = Cesium.Math.toRadians(i);
positions.push(new Cesium.Cartesian2(
radius * Math.cos(radians), radius * Math.sin(radians)));
}
return positions;
}
function computeStar(arms, rOuter, rInner) {
var angle = Math.PI / arms;
var length = 2 * arms;
var positions = new Array(length);
for (var i = 0; i < length; i++) {
var r = (i % 2) === 0 ? rOuter : rInner;
positions[i] = new Cesium.Cartesian2(
Math.cos(i * angle) * r, Math.sin(i * angle) * r);
}
return positions;
}
var redTube = viewer.entities.add({
name : 'Red tube with rounded corners',
polylineVolume : {
positions : Cesium.Cartesian3.fromDegreesArray(
[-85.0, 32.0,
-85.0, 36.0,
-89.0, 36.0]),
shape : computeCircle(60000.0),
material : Cesium.Color.RED
}
});
var greenBox = viewer.entities.add({
name : 'Green box with beveled corners and outline',
polylineVolume : {
positions : Cesium.Cartesian3.fromDegreesArrayHeights(
[-90.0, 32.0, 0.0,
-90.0, 36.0, 100000.0,
-94.0, 36.0, 0.0]),
shape :[new Cesium.Cartesian2(-50000, -50000),
new Cesium.Cartesian2(50000, -50000),
new Cesium.Cartesian2(50000, 50000),
new Cesium.Cartesian2(-50000, 50000)],
cornerType : Cesium.CornerType.BEVELED,
material : Cesium.Color.GREEN,
outline : true,
outlineColor : Cesium.Color.BLACK
}
});
var blueStar = viewer.entities.add({
name : 'Blue star with mitered corners and outline',
polylineVolume : {
positions : Cesium.Cartesian3.fromDegreesArrayHeights(
[-95.0, 32.0, 0.0,
-95.0, 36.0, 100000.0,
-99.0, 36.0, 200000.0]),
shape : computeStar(7, 70000, 50000),
cornerType : Cesium.CornerType.MITERED,
material : Cesium.Color.BLUE,
outline : true,
outlineColor : Cesium.Color.BLACK
}
});
viewer.zoomTo(viewer.entities);
(9) 球和椭球 entity.ellipsoid
球和椭球 entity.ellipsoidvar blueEllipsoid = viewer.entities.add({
name : 'Blue ellipsoid',
position: Cesium.Cartesian3.fromDegrees(-114.0, 40.0, 300000.0),
ellipsoid : {
//可以指定三个轴的半径
radii : new Cesium.Cartesian3(200000.0, 200000.0, 300000.0),
material : Cesium.Color.BLUE
}
});
var redSphere = viewer.entities.add({
name : 'Red sphere with black outline',
position: Cesium.Cartesian3.fromDegrees(-107.0, 40.0, 300000.0),
ellipsoid : {
//正球体
radii : new Cesium.Cartesian3(300000.0, 300000.0, 300000.0),
material : Cesium.Color.RED,
outline : true,
outlineColor : Cesium.Color.BLACK
}
});
var outlineOnly = viewer.entities.add({
name : 'Yellow ellipsoid outline',
position: Cesium.Cartesian3.fromDegrees(-100.0, 40.0, 300000.0),
ellipsoid : {
radii : new Cesium.Cartesian3(200000.0, 200000.0, 300000.0),
fill : false,
outline : true,
outlineColor : Cesium.Color.YELLOW,
slicePartitions : 24, //横向切割线
stackPartitions : 36 //纵向切割线
}
});
(10) 墙 entity.wall
墙 entity.wall/东西方向的横墙
var redWall = viewer.entities.add({
name : 'Red wall at height',
wall : {
positions : Cesium.Cartesian3.fromDegreesArrayHeights(
[-115.0, 44.0, 200000.0,//坐标点
-90.0, 44.0, 200000.0]
),
minimumHeights : [100000.0, 100000.0], //按坐标点的最小高度数组
material : Cesium.Color.RED
}
});
//四边围墙
var greenWall = viewer.entities.add({
name : 'Green wall from surface with outline',
wall : {
positions : Cesium.Cartesian3.fromDegreesArrayHeights(
[-107.0, 43.0, 100000.0,
-97.0, 43.0, 100000.0,
-97.0, 40.0, 100000.0,
-107.0, 40.0, 100000.0,
-107.0, 43.0, 100000.0]),
material : Cesium.Color.GREEN,
outline : true,
outlineColor : Cesium.Color.BLACK
}
});
//曲折的墙
var blueWall = viewer.entities.add({
name : 'Blue wall with sawtooth heights and outline',
wall : {
//坐标点,不指定高度
positions : Cesium.Cartesian3.fromDegreesArray(
[-115.0, 50.0,
-112.5, 50.0,
-110.0, 50.0,
-107.5, 50.0,
-105.0, 50.0,
-102.5, 50.0,
-100.0, 50.0,
-97.5, 50.0,
-95.0, 50.0,
-92.5, 50.0,
-90.0, 50.0]),
maximumHeights : [ //上高
100000, 200000, 100000, 200000, 100000, 200000,
100000, 200000, 100000, 200000, 100000],
minimumHeights : [ //下高
0, 100000, 0, 100000, 0, 100000, 0, 100000, 0,
100000, 0],
material : Cesium.Color.BLUE,
outline : true,
outlineColor : Cesium.Color.BLACK
}
});
(11) 点(Points),公告牌( Billboards), 标注(Labels)
var viewer = new Cesium.Viewer('cesiumContainer');
var citizensBankPark = viewer.entities.add({
name : 'Citizens Bank Park',
position : Cesium.Cartesian3.fromDegrees(-75.166493, 39.9060534),
point : {
pixelSize : 5,
color : Cesium.Color.RED,
outlineColor : Cesium.Color.WHITE,
outlineWidth : 2
},
label : {
text : 'Citizens Bank Park',
font : '14pt monospace',
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
outlineWidth : 2,
verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
pixelOffset : new Cesium.Cartesian2(0, -9)
}
});
viewer.zoomTo(viewer.entities);
点,公告牌, 标注
12. 三维模型
加载三维模型和前面其他的可视数据区别不大。只需要entity带position属性和一个指向glTF模型资源的uri路径。
var viewer = new Cesium.Viewer('cesiumContainer');
var entity = viewer.entities.add({
position : Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706),
model : {
uri : '../../../../Apps/SampleData/models/GroundVehicle/GroundVehicle.glb'
}
});
viewer.trackedEntity = entity;
三维模型
- scale 属性,它将等比例缩放模型。
- minimumPixelSize 属性,它保证距离模型很远的时候,模型不会小于设定的大小
默认,模型向右朝向东方。可以通过 Entity.orientation 的属性设定一个 四元数Quaternion。这个比前面只用位置的示例更麻烦一些,让我们设定一下模型的 heading, pitch, roll
var viewer = new Cesium.Viewer('cesiumContainer');
var position = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706);
var heading = Cesium.Math.toRadians(45.0);
var pitch = Cesium.Math.toRadians(15.0);
var roll = Cesium.Math.toRadians(0.0);
var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, new Cesium.HeadingPitchRoll(heading, pitch, roll));
var entity = viewer.entities.add({
position : position,
orientation : orientation,
model : {
uri : '../../../../Apps/SampleData/models/GroundVehicle/GroundVehicle.glb'
}
});
viewer.trackedEntity = entity;
5. 常用属性
fill:boolean类型,控制表面是否填充;
outline:属性控制是否有外边界;
material:当 fill=true,material属性决定了用什么材质填充表面;默认fill=true, outline=false;
6. material设置
var entity = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-103.0, 40.0),
ellipse : {
semiMinorAxis : 250000.0,
semiMajorAxis : 400000.0,
material : Cesium.Color.BLUE.withAlpha(0.5)
}
});
viewer.zoomTo(viewer.entities);
var ellipse = entity.ellipse;
material设置为蓝色的效果
图片材质
ellipse.material = '//cesiumjs.org/tutorials/images/cats.jpg';
material设置为图片的效果
当设置颜色或者url之后Cesium会自动创建 ColorMaterialProperty 或者ImageMaterialProperty对象。 对于更复杂的材质, 需要手动创建 MaterialProperty对象。 当前, Entity 面和体支持 颜色(colors),纹理图片( images),棋盘 (checkerboard), 条纹(stripe), 网格(grid)等材质.
网格材质
ellipse.material = new Cesium.CheckerboardMaterialProperty({
evenColor : Cesium.Color.WHITE,
oddColor : Cesium.Color.BLACK,
repeat : new Cesium.Cartesian2(4, 4)
});
网络材质
条纹材质
ellipse.material = new Cesium.StripeMaterialProperty({
evenColor : Cesium.Color.WHITE,
oddColor : Cesium.Color.BLACK,
repeat : 32
});
条纹材质
网格材质
ellipse.material = new Cesium.GridMaterialProperty({
color : Cesium.Color.YELLOW,
cellAlpha : 0.2,
lineCount : new Cesium.Cartesian2(8, 8),
lineThickness : new Cesium.Cartesian2(2.0, 2.0)
});
网格材质
7. outline设置
outlineWidth :仅在非windows系统上有效,比如Android, iOS, Linux, 和OS X,Windows系统上边线宽度永远为1。主要是因为三大主流浏览器引擎在windows平台上实现webgl上的技术限制。
outlineColor:
ellipse.fill = false;
ellipse.outline = true;
ellipse.outlineColor = Cesium.Color.YELLOW;
ellipse.outlineWidth = 2.0;
outline
折线是个特例,折线宽度和折线的边线宽度,在所有系统都有效
var entity = viewer.entities.add({
polyline : {
positions : Cesium.Cartesian3.fromDegreesArray([-77, 35,
-77.1, 35]),
width : 5,
material : Cesium.Color.RED
}});
viewer.zoomTo(viewer.entities);
var polyline = entity.polyline // For upcoming examples
折线可设置宽度
8. 相关函数设置
1. 选中和描述
点击Entity将在它的位置会显示 SelectionIndicator 控件,entity设置了name属性,它显示在 InfoBox标题栏,也可以通过 Entity.description 设置一段HTML当作infobox的内容。 把下面的代码追加到上面的示例里:
yoming.description = '\
<img\
width="50%"\
style="float:left; margin: 0 1em 1em 0;"\
src="//cesiumjs.org/tutorials/Visualizing-Spatial-Data/images/Flag_of_Wyoming.svg"/>\
<p>\
Wyoming is a state in the mountain region of the Western \
United States.\
</p>\
<p>\
Wyoming is the 10th most extensive, but the least populous \
and the second least densely populated of the 50 United \
States. The western two thirds of the state is covered mostly \
with the mountain ranges and rangelands in the foothills of \
the eastern Rocky Mountains, while the eastern third of the \
state is high elevation prairie known as the High Plains. \
Cheyenne is the capital and the most populous city in Wyoming, \
with a population estimate of 62,448 in 2013.\
</p>\
<p>\
Source: \
<a style="color: WHITE"\
target="_blank"\
href="http://en.wikipedia.org/wiki/Wyoming">Wikpedia</a>\
</p>';
选中弹出描述
9. 获取Entity
1. 通过 getById来获取Entity对象
var entity = viewer.entities.getById('uniqueId');
2. 如果id不存在就新建,如果id存在就更新
var entity = viewer.entities.getOrCreateEntity('uniqueId');
10. 接收集合里entity被添加、删除甚至更新的通知
function onChanged(collection, added, removed, changed){
var msg = 'Added ids';
for(var i = 0; i < added.length; i++) {
msg += '\n' + added[i].id;
}
console.log(msg);
}
viewer.entities.collectionChanged.addEventListener(onChanged);
11.拾取(也可参考网站cesium学习笔记)
拾取,也就是返回特定屏幕坐标(通常是鼠标位置)的对象
/**
* 返回对应窗口位置最上面一个Entity 如果该位置没有对象那么返回undefined
* @param {Cartesian2} windowPosition 窗口坐标
* @returns {Entity} 返回值
*/
function pickEntity(viewer, windowPosition) {
var picked = viewer.scene.pick(windowPosition);
if (Cesium.defined(picked)) {
var id = Cesium.defaultValue(picked.id, picked.primitive.id);
if (id instanceof Cesium.Entity) {
return id;
}
}
return undefined;
};
/**
*返回对应窗口位置所有Entity的列表 如果该位置没有对象那么返回undefined
* 返回值按可视化顺序从前到后存储在数组里
*
* @param {Cartesian2} windowPosition 窗口位置
* @returns {Entity[]}
*/
function drillPickEntities(viewer, windowPosition) {
var i;
var entity;
var picked;
var pickedPrimitives = viewer.scene.drillPick(windowPosition);
var length = pickedPrimitives.length;
var result = [];
var hash = {};
for (i = 0; i < length; i++) {
picked = pickedPrimitives[i];
entity = Cesium.defaultValue(picked.id, picked.primitive.id);
if (entity instanceof Cesium.Entity &&
!Cesium.defined(hash[entity.id])) {
result.push(entity);
hash[entity.id] = true;
}
}
return result;
};
网友评论