解构开放层3贴图

Deconstructing an Open Layers 3 map

本文关键字:贴图      更新时间:2023-09-26

因此,我使用带有Ember.js的Open Layers 3来制作仪表板,我已经动态加载了地图,但我希望在离开路线时将其销毁,我找到的唯一东西是map.dedestroy(),但它是用于API的旧版本,新版本中似乎没有。

在访问了几次地图页面后,我使用了chrome调试器,发现我有29 ol。映射对象。

这就是我目前拥有的

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

我在文档中找不到任何关于删除地图的信息。

提前谢谢。

您应该尝试这样做:

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

它将映射与其元素分离,并允许正确的垃圾收集。不要忘记删除对地图对象的任何其他引用(如果有的话)。