简体   繁体   English

需要在OL3中克隆图层

[英]Need to clone layer in OL3

How can I clone a layer in OL3 ? 如何在OL3中克隆图层?

For the moment I try this but it's giving me a different object with different aspect of a real ol.layer. 目前,我尝试了此操作,但它为我提供了一个具有真正ol.layer不同方面的不同对象。 Like you can see with inside console log I lost the instance of the layer and it's important for me to keep it. 就像您在内部控制台日志中看到的那样,我丢失了该层的实例,这对我来说很重要。

 <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://openlayers.org/en/v3.20.1/css/ol.css" type="text/css"> <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x --> <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://openlayers.org/en/v3.20.1/build/ol.js"></script> </head> <body> <div id="map" class="map" tabindex="0"></div> <script> var map = new ol.Map({ layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], target: 'map', view: new ol.View({ center: [0, 0], zoom: 2 }) }); console.log("map layer is an instanceof ol.layer.tile? ", map.getLayers().getArray()[0] instanceof ol.layer.Tile) function cloneLayer() { map.getLayers().getArray().forEach(function(layer) { var copyLayer = jQuery.extend(true, {}, layer); console.log("CopyLayer is an instanceof ol.layer.tile? ", copyLayer instanceof ol.layer.Tile) console.log("CopyLayer is equal to mapLayer? ", map.getLayers().getArray()[0] == copyLayer) }); } cloneLayer() </script> </body> </html> 

I'm trying to find a situation that you will need clone a layer and i don't find it. 我试图找到一种情况,您将需要克隆一个图层,但我找不到它。

Many other classes in ol3 has their method clone to do it. ol3中的许多其他类都有其方法clone来实现。 Clonning a layer involve copy your source, that has features and many many many other objects. 复制层涉及复制您的源代码,该源代码具有功能以及许多其他许多对象。

The jQuery.extend work's fine for DOM elements, it was made to DOM objects, see docs , for complex objects that are instances of custom classes, prototyped classes, like all ol classes are made off, clone an object is much more complex than iterate its properties and copy each one. jQuery.extend工作非常适合DOM元素,它是针对DOM对象制作的, 请参阅docs ,对于作为自定义类实例的原型的复杂对象,原型类(如取消所有ol类)而言,克隆对象要比迭代复杂得多其属性并复制每个属性。

So, i suggest that you create a new layer and just copy their options, if you need another layer with same characteristics. 因此,如果您需要具有相同特征的另一层,我建议您创建一个新层并仅复制其选项。

Or, if you could explain the situation that you need to clone a layer, maybe we can have an alternative solution. 或者,如果您可以解释需要克隆图层的情况,也许我们可以有一个替代解决方案。

I am not familliar with OL3 but maybe you could try to use your original object as the prototype of the new one with 我对OL3不熟悉,但是也许您可以尝试将原始对象用作新对象的原型,

var copyLayer = Object.create(layer.prototype) var copyLayer = Object.create(layer.prototype)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM