简体   繁体   English

使用传单如何删除外部插件添加的旧地图图块?

[英]Using leaflet how can you remove old map tiles added by external plugins?

I have a map with the feature set to change your map tiles in user preferences, the below function is responsible for changing the map tiles dynamically when the user wants to.我有一张地图,其功能集可以在用户首选项中更改地图图块,以下函数负责在用户需要时动态更改地图图块。

  • The actual problem实际问题

When I do change the map tiles, for example from CartoDB to OSM in real time, when planning the map viewport, you can see old tiles from CartoDB.当我更改地图图块时,例如从 CartoDB 实时更改为 OSM,在规划地图视口时,您可以看到来自 CartoDB 的旧图块。

Evidence / Video of this https://i.gyazo.com/702e8e4875bc89c13f41a132dccb78da.mp4证据/视频https://i.gyazo.com/702e8e4875bc89c13f41a132dccb78da.mp4

Another thing I should mention, I'm using "leaflet-providers" plugin, for the simple API of adding the tiles to the map.我应该提到的另一件事是,我正在使用“leaflet-providers”插件,用于将图块添加到地图的简单 API。

I did raise an issue on the github repo about my experience of this, but the issue was closed and the author mentioned that this was indeed a leaflet issue, so I've come here for some leaflet.js advice :)我确实在 github repo 上提出了一个关于我的经验的问题,但该问题已关闭,作者提到这确实是一个传单问题,所以我来这里是为了寻求一些leaflet.js 建议:)

I am aware of how to remove tile layers already, so I need something a little more specific and in relation to my code, here is what I've tried.我已经知道如何删除平铺层,所以我需要一些更具体的和与我的代码相关的东西,这是我尝试过的。

//this

self.map.removeLayer(L.tileLayer.provider('OpenStreetMap'))

//this?
//self.map.invalidateSize();

//this??
self.map.invalidateSize();
let self = this;
let mapLayer = self.mapLayer;

if (mapLayer === 'osm') {
    L.tileLayer.provider('OpenStreetMap').addTo(self.map);
}
if (mapLayer === 'cartodb') {
    L.tileLayer.provider('CartoDB').addTo(self.map);
}

//attempt to refresh tiles?
self.map._onResize();
self.map.invalidateSize();

What happens is that whenever you call L.tileLayer.provider('OpenStreetMap') , you get a new instance of a Leaflet Tile Layer.发生的情况是,每当您调用L.tileLayer.provider('OpenStreetMap') ,您都会获得一个 Leaflet Tile Layer 的实例。

Therefore when you try removing a Layer from the map with map.removeLayer(L.tileLayer.provider('OpenStreetMap')) , you pass a new layer to your map instance.因此,当您尝试使用map.removeLayer(L.tileLayer.provider('OpenStreetMap'))从地图中删除图层时, map.removeLayer(L.tileLayer.provider('OpenStreetMap'))图层传递给map实例。 Because this new Layer is not yet on the map, nothing happens.因为这个新图层还没有出现在地图上,所以没有任何反应。 In particular, the previous Tile Layer remains in place, leading to the situation where you can still see its tiles occasionally.尤其是之前的瓦片层还在原地不动,导致偶尔还能看到它的瓦片的情况。

Simply keep a reference to your initial Layer and use that reference to remove it from the map later on:只需保留对初始图层的引用,然后使用该引用将其从地图中删除:

var myLayer = L.tileLayer.provider('OpenStreetMap');

myLayer.addTo(map);

// Later...
map.removeLayer(myLayer);

// even simpler alternative:
myLayer.remove();

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

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