简体   繁体   English

leaflet:刷新没有缓存的tilelayer

[英]leaflet: refresh tilelayer without cache

I am loading a tile layer as overlay into my leaflet map and my goal is to refresh the layer every n-minutes. 我正在将一个图块层加载到我的传单地图中,我的目标是每隔n分钟刷新一次图层。

I use the redraw(); 我用redraw(); method but I figured out, that the data always came from the cache and not from the server. 方法,但我发现,数据总是来自缓存而不是来自服务器。 after I zoom in or out of the map the tiles will be sometimes requested from the server. 放大或缩小地图后,有时会从服务器请求切片。

the code for the layer and the refresh looks like this: 图层和刷新的代码如下所示:

var TomTom_Incident = L.tileLayer('https://api.tomtom.com/traffic/map/4/tile/incidents/s3/{z}/{x}/{y}.png?key=<APIKEY>', {
    maxZoom: 18,
    attribution: '&copy; <a href="https://www.tomtom.com/" target="_blank">TomTom</a>',
    opacity: 0.85
});

var TomTom_Incident_intervall = "";

function refresh_TomTom_Incident() {

    TomTom_Incident.redraw();

    console.log(consoleLogTime.getHours()+':'+consoleLogTime.getMinutes()+':'+consoleLogTime.getSeconds()+'TomTom_Incident Intervall active');
}


TomTom_Incident.on('add', function(e) {

    TomTom_Incident_intervall = setInterval(refresh_TomTom_Incident, 300000);

    console.log('TomTom_Incident added');
});

if (map.hasLayer(TomTom_Incident)) {
    console.log('TomTom_Incident present');

    TomTom_Incident_intervall = setInterval(refresh_TomTom_Incident, 300000);

}   else {
    console.log('TomTom_Incident not present');
}

TomTom_Incident.on('remove', function(e) {
    clearInterval(TomTom_Incident_intervall);
    console.log('Intervall active');
});

is there a way to always request the tiles after redraw(); 有没有办法在redraw();之后总是请求tile redraw(); or to disable the cache for this tile layer? 或者为此图块层禁用缓存?

link to the docs: https://developer.tomtom.com/online-traffic/online-traffic-documentation-online-traffic-incidents/traffic-incident-tiles 链接到文档: https//developer.tomtom.com/online-traffic/online-traffic-documentation-online-traffic-incidents/traffic-incident-tiles

thanks in advance! 提前致谢!

Start by checking out: http://leafletjs.com/reference-1.3.0.html#tilelayer . 首先退房: http//leafletjs.com/reference-1.3.0.html#tilelayer As you can see, you can "use custom keys in the template, which will be evaluated from TileLayer options". 如您所见,您可以“在模板中使用自定义键,这些键将从TileLayer选项中进行评估”。

What you should do is define your layer so that it appends a random custom key to your URL. 您应该做的是定义您的图层,以便它为您的网址添加随机自定义键。 This way that browser will not recognize the tiles as having been previously cached. 这样浏览器就不会将磁贴识别为先前已缓存过的。

I've implemented an example on Plunker for you: http://plnkr.co/edit/Bux8bM30WtVCklOL7ndr?p=preview 我已经为你实现了一个关于Plunker的例子: http ://plnkr.co/edit/Bux8bM30WtVCklOL7ndr?p = preview

Here's the operative section: 这是执行部分:

 var generateRandInt = function() { return Math.floor( Math.random() * 200000 ) + 1; }; L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?{randint}, { randint: generateRandInt, maxZoom: 18, attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' + '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' + 'Imagery © <a href="http://mapbox.com">Mapbox</a>', id: 'mapbox.streets' }).addTo(mymap); 

Of course, this will generate a new random integer for each tile request. 当然,这将为每个tile请求生成一个新的随机整数。 If you want to use different integer for each redraw action, then you can imagine doing something like this: 如果你想为每个重绘动作使用不同的整数,那么你可以想象做这样的事情:

 var redrawint = Math.floor( Math.random() * 200000 ) + 1 var getRedrawInteger = function() { return redrawint; }; var incrementRedrawInteger = function() { redrawint += 1; }; L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?{randint}, { randint: getRedrawInteger, maxZoom: 18, attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' + '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' + 'Imagery © <a href="http://mapbox.com">Mapbox</a>', id: 'mapbox.streets' }).addTo(mymap); 

So, you'd call incrementRedrawInteger() before each redraw. 因此,您需要在每次重绘之前调用incrementRedrawInteger()。 This would essentially invalidate your cache for the redraw. 这实际上会使重绘的缓存无效。

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

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