drillPick(windowPosition,limit,width,height)→Array.<*>
scene中(一般用来获取entity对象):
pick:scene.pick可以通过此方法获取到pick对象,通过pick.id即可拾取当前的entity对象,也可以获取Cesium3DTileFeature
对象;
drillPick:scene.drillPick(click.position)是从当前鼠标点击位置获取entity的集合,然后通过for循环可以获取当前坐标下的所有entity;
pickPosition:通过viewer.scene.pickPosition(movement.position)获取,可以获取场中任意点击处的对应的世界坐标。(高程不精确)
pick与drillPick的区别:pick只可获取一个entity对象(如该位置存在多个entity,哪怕面点线不在同一高度,面entity都可能会盖住点线entity),但drillPick可获取当前坐标下的多个对象;
camera和globel中的pick:
这两个里面的pick一般搭配使用,通过camera中的getPickRay获取ray(射线),然后通过globel中的pick方法,获取世界坐标,如下面的地形坐标的获取;
======================================================
1、通过pick进行地形上的坐标的获取
这个是常用的方法,当你想获取当前鼠标位置的三维坐标时,经常使用到这个方法:
第一步:通过camera的getPickRay,将当前的屏幕坐标转为ray(射线);
viewer.camera.getPickRay(windowCoordinates);
第二步:找出ray和地形的交点,即可求出三维世界坐标
globe.pick(ray, scene);
2、通过pick获取entity
handler.setInputAction(function (movement) {
var pick = viewer.scene.pick(movement.endPosition); //获取的pick对象
var pickedEntity = Cesium.defined(pick) ? pick.id : undefined; //pick.id即为entity
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
Picking 拾取
选择(单击选择一个对象)是我们需要与基本API进行短暂交互的领域之一。使用scene.pick和scene.drillpick检索实体。
/*** Returns the top-most entity at the provided window coordinates* or undefined if no entity is at that location.** @param {Cartesian2} windowPosition The window coordinates.* @returns {Entity} The picked entity or undefined.*/function pickEntity(viewer, windowPosition) {var picked = viewer.scene.pick(windowPosition);if (defined(picked)) {var id = Cesium.defaultValue(picked.id, picked.primitive.id);if (id instanceof Cesium.Entity) {return id;}}return undefined;};/*** Returns the list of entities at the provided window coordinates.* The entities are sorted front to back by their visual order.** @param {Cartesian2} windowPosition The window coordinates.* @returns {Entity[]} The picked entities or undefined.*/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;};
Cesium中使entity响应drillPick点击事件
var handler = new Cesium.ScreenSpaceEventHandler(canvas);
handler.setInputAction(function(click){
var pickedObjects = scene.drillPick(click.position);
for(var objNumber=0;objNumber
var pickedId=pickedObject.id.id;}}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
注意scene.drillPick(click.position)是从当前鼠标点击位置获取entity的集合pickedObjects,通过一个for循环,用pickedId匹配所要响应的entity的id。如果把LEFT_CLICK换成MOUSE_MOVE或者其他,则需要考虑click的position是什么。比如,MOUSE_MOVE的click就有startPosition和endPosition,请具体问题具体分析。
如果把scene.drillPick(click.position)中drillPick改成pick,那么当鼠标点击或滑过时该位置存在多个entity,哪怕面点线不在同一高度,面entity都可能会盖住点线entity,从而使得被遮盖的点或线无法响应点击和滑过事件。