问题
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