none
TileLayer.setOptions doesn't update TileSource uri RRS feed

  • Question

  • I have a TileLayer with an existing TileSource set. I append a timestamp to my TileSource URL so that the browser doesn't cache the tiles. This means I have to periodically update the TileSource's URL. So every few minutes I do this:

    map.layers.remove(tileLayer);

    var url = "http://geo.weather.gc.ca/geomet/?LANG=E&SERVICE=WMS&REQUEST=GetMap&FORMAT=image/png&SRS=EPSG:4326&WIDTH=256&HEIGHT=256&TRANSPARENT=TRUE&STYLE=RADARURPPRECIPS,RADARURPPRECIPR&LAYERS=RADAR_RSNO,RADAR_RRAI&BBOX={bbox}&NOCACHE={timestamp}"

    url = url.replace("{timestamp}", Date.now());
    var tileLayerOptions = {
    visible: true,
    mercator: new Microsoft.Maps.TileSource({
    uriConstructor: url
    })
    };
    tileLayer.setOptions(tileLayerOptions);
    map.layers.insert(tileLayer);

    The problem is that the TileSource is never updated. If I check tileLayer.getTileSource().getUriConstructor() after doing tileLayer.setOptions, it will still show the old URL.

    Is this something that can be fixed soon?


    • Edited by Peter____K Friday, January 6, 2017 6:26 PM
    Friday, January 6, 2017 6:20 PM

Answers

  • If the URL is 100% the same each time, then caching will likely occur. What you can do is add a random URL parameter to remove caching. When you create the tile layer, do something like the following to the URL:  

    var url = "Your current URL" + "&noCache=" + Date.now();

    Friday, June 22, 2018 4:40 PM
  • This is by design. If you look at the other functions for the TileLayer class only the opacity, visible and zIndex options can be set. The mercator property (TileSource) can't be changed as the TileLayer class optimizes itself based on previously loaded tiles from the tile source. Instead, simply create a new tile layer and remove/insert from the map as needed. I'll add a bit more details in the documentation to make this clearer. Note, this is also how the previous version of Bing Maps worked as well.

    [Blog] [twitter] [LinkedIn]

    Monday, January 9, 2017 7:32 PM

All replies

  • This is by design. If you look at the other functions for the TileLayer class only the opacity, visible and zIndex options can be set. The mercator property (TileSource) can't be changed as the TileLayer class optimizes itself based on previously loaded tiles from the tile source. Instead, simply create a new tile layer and remove/insert from the map as needed. I'll add a bit more details in the documentation to make this clearer. Note, this is also how the previous version of Bing Maps worked as well.

    [Blog] [twitter] [LinkedIn]

    Monday, January 9, 2017 7:32 PM
  • Ricky,

    I have this same issue.  In fact the TileLayer containing a weather image from NOAA is surviving (caching) through even a browser refresh.  In debug mode I am watching it recreate the entire map including all the layers.  Yet this cached image remains. I am assuming it gets named and stored locally.

    If the user logs off the application and logs back in they get a new version of the weather layer.

    The weather layer updates every three minutes.

    Can you help please?

    Friday, June 22, 2018 4:11 PM
  • If the URL is 100% the same each time, then caching will likely occur. What you can do is add a random URL parameter to remove caching. When you create the tile layer, do something like the following to the URL:  

    var url = "Your current URL" + "&noCache=" + Date.now();

    Friday, June 22, 2018 4:40 PM
  • Yes, that did it, thanks! And so quick!

    By the way. Why not allow those who ask to mark as answered? After all, today's answer was much more effective than the one over a year ago. Creating a new tile layer does nothing to refresh the cached image, yet discussion gets cut off, affectively.

    Friday, June 22, 2018 5:31 PM