Reputation: 142
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.com/traffic/map/4/tile/incidents/s3/{z}/{x}/{y}.png?key=<APIKEY>', {
maxZoom: 18,
attribution: '© <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();
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
thanks in advance!
Upvotes: 4
Views: 5254
Reputation: 472
Start by checking out: 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".
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.com/v4/{id}/{z}/{x}/{y}.png?{randint}, {
randint: generateRandInt,
maxZoom: 18,
attribution: 'Map data © <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. 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 © <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. This would essentially invalidate your cache for the redraw.
Upvotes: 5