javascript-传单:刷新不带缓存的tilelayer

我正在将图块图层作为叠加加载到我的传单地图中,我的目标是每n分钟刷新一次该图层.

我使用redraw();方法,但我发现,数据始终来自缓存而不是来自服务器.
在我放大或缩小地图后,有时会从服务器请求磁贴.

图层和刷新的代码如下所示:

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

有没有办法总是在redraw()之后请求磁贴;或禁用此图块层的缓存?

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

提前致谢!

解决方法:

首先检出:http://leafletjs.com/reference-1.3.0.html#tilelayer.如您所见,您可以“在模板中使用自定义键,将从TileLayer选项中对其进行评估”.

您应该做的是定义图层,以便将随机的自定义键添加到您的URL.这样,浏览器就不会将图块识别为先前已缓存.

我已经为您实现了一个关于Plunker的示例:http://plnkr.co/edit/Bux8bM30WtVCklOL7ndr?p=preview

这是手术部分:

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

当然,这将为每个图块请求生成一个新的随机整数.如果您想为每个重绘操作使用不同的整数,那么您可以想象这样做:

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

因此,您需要在每次重绘之前调用crementRedrawInteger().这实际上会使重新绘制的缓存失效.

上一篇:leaflet 结合 Echarts4 实现统计图(附源码下载)


下一篇:javascript – 宣传单工具提示无法正确制作动画