将选定的对象添加到数组将返回未定义的内容,但当前铯铯中的对象除外 [英] adding selected object to array returns undefined except for the current one in Cesium

查看:72
本文介绍了将选定的对象添加到数组将返回未定义的内容,但当前铯铯中的对象除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数以在Cesium中选择对象. 我的问题是,当我选择第一个对象时,它保存在数组"Selected [0]"中,但是当我选择第二个对象时,一个值将存储在"Selected 1 "而"Selected [0]"变得不确定!

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!

我需要一个数组来记住所有选中的对象并为它们提供所有突出显示的颜色,现在它只更改当前选中的颜色,即保存在"Selected"数组中的颜色

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

这是我的代码,位于柱塞

在下面的代码部分中,我已经厌倦了这两种方法来填充我的数组,但它们都不起作用.

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);

推荐答案

这不是铯的问题-您的代码中有错误. 在代码中,您可以在事件处理程序中初始化选定的数组:

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);

注意行selected = [];.每次单击都会创建一个新的空selected数组.只需将其放在事件处理程序之外,您的代码即可按预期工作(只要涉及问题所在).

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).

这篇关于将选定的对象添加到数组将返回未定义的内容,但当前铯铯中的对象除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆