ThinkGeo.com    |     Documentation    |     Premium Support

How to force LayerOverlay (OSM, WMS, WMTS) into offline-only mode (cache only, no network requests)?

Hello,

I am working on a ThinkGeo Android project where I use several types of base maps:

  • OpenStreetMapAsyncLayer
  • WMS layers
  • WMTS layers

All of them are displayed inside a LayerOverlay , and all of them use a FileRasterTileCache .

What I need is a reliable way to switch the map into offline-only mode , meaning:

  • the map must use only the tiles already stored in the cache
  • no network requests should be made at all
  • tiles that are missing in the cache should simply appear blank or show the “NoDataTileImage”
  • then be able to switch back to normal online mode later

I initially expected something like a property (e.g. “CacheOnly”, “OfflineOnly”, “DisableNetwork”), but LayerOverlay and the tile-based layers do not expose anything like this.
I also could not find any built-in way to intercept or block tile downloads at the overlay level.

Since all these layers rely on the internal tile request pipeline, I would like to know:

What is the official ThinkGeo-supported way to run a LayerOverlay (OSM, WMS, WMTS) in cache-only / offline mode?

Specifically:

  1. How can I prevent OpenStreetMapAsyncLayer from downloading tiles and force it to read only from FileRasterTileCache?
  2. How can I apply the same offline behavior to WMSLayer and WMTSLayer?
  3. Is there an API I missed that allows LayerOverlay to operate strictly from cache without performing any HTTP requests?
  4. If not, what is ThinkGeo’s recommended approach to implement offline-only mode for these tile layers?

I found discussions about PreCaching APIs, but that only addresses how to fill the cache in advance—it does not disable online requests.

Thanks a lot for your help.

BR

Hi, BR

  1. TileOverlay(and the subclass LayerOverlay) has a property IsCacheOnly, set it to true to use the cache tile only. The code will be like this:
      var overlay = new LayerOverlay();
      overlay.Layers.Add(new OpenStreetMapAsyncLayer("thinkgeo-test"));
      androidMap.Overlays.Add(overlay);

      string cacheDir = Application.Context.CacheDir?.AbsolutePath;
      overlay.IsCacheOnly = true;
      overlay.TileCache = new FileRasterTileCache(cacheDir, "layerOverlay");

Let me know your version number if you cannot find it.

  1. By default, the map just draw blank tiles if not exist in the cache. You can create your own FileRasterTileCache to change it, like following:
  class CustomFileRasterTileCache : FileRasterTileCache
  {
      public CustomFileRasterTileCache(string cacheDirectory, string cacheId)
          : base(cacheDirectory, cacheId)
      {
      }

      protected override Tile GetTileCore(int zoom, long x, long y)
      {
          var tile = base.GetTileCore(zoom, x, y);
          if (tile.Content == null)
              // return your custom NoTileImage
              tile.Content = File.ReadAllBytes("NoTileImage.png");
          return tile;
      }
  }
  1. I think #1 and #2 should be enough for you. Here just FYI you can also disable the Http request in the Layer Level by setting args.Cancel to true iin the SendingHttpRequest event, like following:
var layer = new OpenStreetMapAsyncLayer();
layer.SendingHttpRequest += (sender, args) => args.Cancel = true;

I hope that helps.

Thanks,
Ben