最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - leaflet: refresh tilelayer without cache - Stack Overflow

programmeradmin0浏览0评论

I am loading a tile layer as overlay into my leaflet map and my goal is to refresh the layer every n-minutes.

I use the 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('/{z}/{x}/{y}.png?key=<APIKEY>', {
    maxZoom: 18,
    attribution: '&copy; <a href="/" 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(); or to disable the cache for this tile layer?

link to the docs:

thanks in advance!

I am loading a tile layer as overlay into my leaflet map and my goal is to refresh the layer every n-minutes.

I use the 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./traffic/map/4/tile/incidents/s3/{z}/{x}/{y}.png?key=<APIKEY>', {
    maxZoom: 18,
    attribution: '&copy; <a href="https://www.tomtom./" 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(); or to disable the cache for this tile layer?

link to the docs: https://developer.tomtom./online-traffic/online-traffic-documentation-online-traffic-incidents/traffic-incident-tiles

thanks in advance!

Share Improve this question asked Apr 11, 2018 at 13:18 wuk986wuk986 1423 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Start by checking out: http://leafletjs./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".

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

Here's the operative section:

var generateRandInt = function() {
    return Math.floor( Math.random() * 200000 ) + 1;
};

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

Of course, this will generate a new random integer for each tile request. 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./v4/{id}/{z}/{x}/{y}.png?{randint}, {
		randint: getRedrawInteger,
		maxZoom: 18,
		attribution: 'Map data &copy; <a href="http://openstreetmap">OpenStreetMap</a> contributors, ' +
			'<a href="http://creativemons/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
			'Imagery © <a href="http://mapbox.">Mapbox</a>',
		id: 'mapbox.streets'
}).addTo(mymap);

So, you'd call incrementRedrawInteger() before each redraw. This would essentially invalidate your cache for the redraw.

发布评论

评论列表(0)

  1. 暂无评论