JavaScript - 如何创建一个包含引用对象的数组?
我正在使用 JavaScript 映射库 - OpenLayer 创建标记叠加。 我想动态控制标记:添加新标记并从图层中删除现有标记。
向图层添加新标记的方式是通过命令:
markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0),icon));
如您所见,初始化参数仅包含坐标和图标图像,而不包含 id,甚至不作为可选参数。
为了控制标记,我想创建二维数组,其中包含标记数组通过引用和ID数组。
然后,当我想从图层中删除标记时,命令将很简单:
markers.removeMarker(ArrayMarkers[i]);
如何通过引用将元素推送到 JavaScript 数组?
如何通过引用在 ArrayMarkers 元素上运行?
I'm using JavaScript Mapping Library - OpenLayer to create a markers overlay.
I want to control the markers dynamically: add new ones and remove existing markers from the layer.
the way to add a new marker to the layer is by the command:
markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0),icon));
as you can see, the initialization parameters contain only coordinates and icon image, not id, even not as an optional parameter.
in order to control the markers I want to create 2 dimensional array, that contain markers array by reference and ID array.
then, when I want to remove a marker from the layer, the command will be simply:
markers.removeMarker(ArrayMarkers[i]);
How do I push an element to JavaScript array by reference?
How can I run on ArrayMarkers elements by reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
基本上,JavasScript 通常使用引用来传递对象。您已经将引用传递到库中。您可以使用相同的技术。
Try:
Basically, JavasScript generally uses references to pass objects around. You're already passing a reference in to the library. You can use the same technique.
在 JavaScript 中,您不能选择按值或按引用推送变量...这是根据变量的类型自动完成的。
我猜你的标记是对象。因此它们将通过引用被推入数组中。
id 是字符串,它们将按值推送。
In JavaScript, you can't chose to push an variable by value of by reference... this is done automatically depending on the type of the variable.
I guess your markers are Objects. So they will be pushed in the array by reference.
The ids are String, they will be pushed by value.