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

javascript - How do I force a leaflet map to reload all tiles including visible ones? - Stack Overflow

programmeradmin2浏览0评论

I'm working on a graphing web-app and I've decided that leaflet would make a decent graph view. I have it displaying (sort of) but I need a way to force it to update when the user enters a new formula to graph.

I'm using JQuery as well, but that shouldn't matter. Here is the relevant code:

function formulaChange(formula){
     //submits a request to the server to add a graph to display
     map.setView(map.getCenter(),map.getZoom(),true);//doesn't work
     //and neither does:
     //map.fire('viewreset');
     //tiles.redraw();
}

function enterHandler(event){
    if(event.keyCode==13){
        formulaChange(document.getElementById("formula").value);
    }

}

var map;
var tiles;
$(document).ready(function(){
    map=L.map('plot',{crs:L.CRS.Simple}).setView([0,0],10);
    //url is actually a servlet on the server that generates an image on the fly
    tiles = L.tileLayer('./GraphTile.png?x={x}&y={y}&z={z}&tilesize={tileSize}&{s}', 
    {
        maxZoom: 20,
        continuousWorld: true,
        tileSize: 128,
        //subdomains used as a random in the URL to prevent caching
        subdomains: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
    }
    ).addTo(map);
});

This works but won't refresh when the user clicks, the event is definitely running (I've omitted other code that updates a text display). It displays properly, but when the user adds a function to display the view never updates and leaflet continues to display cached images, only a new zoom level or panning to an area never before viewed causes it to update the tiles. The question I have is: How do I force leaflet to completely reload everything and drop and reload all the images?

EDIT added another failed attempt

I'm working on a graphing web-app and I've decided that leaflet would make a decent graph view. I have it displaying (sort of) but I need a way to force it to update when the user enters a new formula to graph.

I'm using JQuery as well, but that shouldn't matter. Here is the relevant code:

function formulaChange(formula){
     //submits a request to the server to add a graph to display
     map.setView(map.getCenter(),map.getZoom(),true);//doesn't work
     //and neither does:
     //map.fire('viewreset');
     //tiles.redraw();
}

function enterHandler(event){
    if(event.keyCode==13){
        formulaChange(document.getElementById("formula").value);
    }

}

var map;
var tiles;
$(document).ready(function(){
    map=L.map('plot',{crs:L.CRS.Simple}).setView([0,0],10);
    //url is actually a servlet on the server that generates an image on the fly
    tiles = L.tileLayer('./GraphTile.png?x={x}&y={y}&z={z}&tilesize={tileSize}&{s}', 
    {
        maxZoom: 20,
        continuousWorld: true,
        tileSize: 128,
        //subdomains used as a random in the URL to prevent caching
        subdomains: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
    }
    ).addTo(map);
});

This works but won't refresh when the user clicks, the event is definitely running (I've omitted other code that updates a text display). It displays properly, but when the user adds a function to display the view never updates and leaflet continues to display cached images, only a new zoom level or panning to an area never before viewed causes it to update the tiles. The question I have is: How do I force leaflet to completely reload everything and drop and reload all the images?

EDIT added another failed attempt

Share Improve this question edited Feb 24, 2013 at 3:32 MatrixPeckham asked Feb 24, 2013 at 2:35 MatrixPeckhamMatrixPeckham 4471 gold badge4 silver badges16 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 13

I found the answer. Despite the no-cache headers my browser was caching the images anyway. The subdomains are not "randomly chosen" as the documentation claims, they are generated using a hash of the tile location. So I had to improvise a way to add "&RANDOM##" to the end of the URL instead of the subdomain.

The new code looks like this:

function enterHandler(event){
    if(event.keyCode==13){
        formulaChange(document.getElementById("formula").value);
    }
}
function formulaChange(formula){
    val.item=Math.random();
    tiles.redraw();
}
var map;
var tiles;
var val={
    item: Math.random(),
    toString: function(){
        return this.item;
    }
};
$(document).ready(function(){
    map=L.map('plot',{crs:L.CRS.Simple}).setView([0,0],10);
    tiles = L.tileLayer('./GraphTile.png?x={x}&y={y}&z={z}&tilesize={tileSize}&{test}', 
    {
        maxZoom: 20,
        continuousWorld: true,
        tileSize: 128,
        test: val
    }
    ).addTo(map);
});

Hope this helps someone else. Please comment if there's a better way to do this.

You can simplify this a bit by using a function instead of the global for the cache buster.

In your case, you can remove the val global var and change the document ready function call to look something like:

$(document).ready(function(){
    map=L.map('plot',{crs:L.CRS.Simple}).setView([0,0],10);
    tiles = L.tileLayer('./GraphTile.png?x={x}&y={y}&z={z}&tilesize={tileSize}&{test}', 
    {
        maxZoom: 20,
        continuousWorld: true,
        tileSize: 128,
        test: Math.random()
    }).addTo(map);
});
发布评论

评论列表(0)

  1. 暂无评论