adding selected object to array returns undefined except for the current one in Cesium

99封情书 提交于 2019-12-11 17:06:28

问题


I'm trying to write a function to multi select object in Cesium. my problem is that when I select the first object, it is saved in the array "Selected[0]" but when I select the second object, a value will be stored in "Selected1" while "Selected[0]" becomes undefined!

I need the array to remember all the selected objects and give them all the highlighted color, for now it only changes the color for the currently selected, the one saved in the "Selected" array

Here is my code in Plunker

In the following part of code, I have tired both methods to populate my array, they both don't work.

 pickedObject = scene.pick(click.position);
 console.log ("picked  "+ pickedObject);
//selected [i]= pickedObject.id.id;
selected.push(pickedObject.id.id);

回答1:


This isn't a Cesium issue - you have an error in your code. In your code, you initialize the selected array inside the event handler:

handler.setInputAction(function(click) {
  var selected = [];
  var pickedObject = [];    
  var list ;

 pickedObject = scene.pick(click.position);
 selected [i]= pickedObject.id.id;

 console.log ("select0  " + selected[0]);
 console.log ("select1  " + selected[1]);
 console.log ("select  " + selected.length);

 for (var j=0; j< selected.length; j++){
  console.log("content: " +selected [j]);
 }
 console.log ("i:  " + i); 
 if (Cesium.defined(pickedObject) ) {
  highlightedEntity = pickedObject.id;
 }
  i++;
},Cesium.ScreenSpaceEventType.LEFT_CLICK, Cesium.KeyboardEventModifier.CTRL);

Note the line selected = [];. It creates a new empty selected array on each click. Just put it outside your event handler and your code should work as expected (as far as the question's issue is involved).



来源:https://stackoverflow.com/questions/46294906/adding-selected-object-to-array-returns-undefined-except-for-the-current-one-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!