解构Open Layers 3地图 [英] Deconstructing an Open Layers 3 map
问题描述
因此,我将Elayer.js与Open Layers 3一起使用来制作仪表板,并且动态加载了地图,但我希望在离开路线时将其销毁,我发现的唯一东西就是地图.destroy(),但是对于旧版本的API而言,新版本中似乎没有一个.
So, I am using Open Layers 3 with Ember.js to make a dashboard, and I've made the map load dynamically but I want it do be destroyed when I leave the route, the only thing I found was map.destroy() but its for an old version of the API and there don't seem to be one in the new version.
几次访问地图页面后,我使用了chrome调试器,发现我有29个ol.Map对象.
I used the chrome debugger after going to the map page a couple of times and found that I had 29 ol.Map objects.
这是我到目前为止所拥有的
This is what I have so far
App.MapView = Ember.View.extend({
map: null,
didInsertElement: function() {
this.map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
})
],
view: new ol.View({
center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
zoom: 4
})
});
},
willDestroyElement: function() {
// destroy this.map
}
});
我在文档中找不到有关删除地图的任何内容.
I cant find anything in the docs about removing maps.
谢谢.
推荐答案
您应该尝试执行以下操作:
You should try to do something like this:
App.MapView = Ember.View.extend({
// if you are not using Ember.get/set you'd better make this "private"
_map: null,
didInsertElement: function() {
this._map = new ol.Map(...);
},
willDestroyElement: function() {
this._map.setTarget(null);
this._map = null;
}
});
它将地图与其元素分离,并允许正确的垃圾回收.不要忘记删除对地图对象的任何其他引用(如果有的话).
It detach the map from its element and allows correct garbage collection. Don't forget to remove any other references to the map object too if any.
这篇关于解构Open Layers 3地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!