Skip to content

Download Tiles & Store Them For Offline Use #795

@JaffaKetchup

Description

@JaffaKetchup

Hello,
I have been trying to use flutter_map to create an app where a specific 'Downloads Only' mode can be enabled. The aim is to have tiles loaded whilst not in this mode downloaded to the users device, like caching (except I do not want these tiles to be deleted by the system). Then, once the user is offline or in 'Downloads Only' mode, I'd like to use the images stored from before.

I also have setup multiple map tile servers and have a global variable holding the app's document directory 'globals.saveDir'. I have started doing this using the network_to_file_image package like so:

class AdvancedFileCacheProvider extends TileProvider {
  @override
  ImageProvider getImage(Coords<num> coords, TileLayerOptions options) {
    return NetworkToFileImage(
      url: getTileUrl(coords, options),
      file: File(
        p.join(
            globals.saveDir.path,
            getTileUrl(coords, options)
                .replaceAll('https://', '')
                .replaceAll('http://', '')),
      ),
      debug: true,
    );
  }
}
...
TileLayerOptions(
          urlTemplate: Uri.decodeQueryComponent(urlForMap),
          subdomains: ['a', 'b', 'c'],
          tileProvider: AdvancedFileCacheProvider(),
),
...

This setup prints:

I/flutter (14390): Fetching image from: http://mt.google.com/vt/lyrs=m&x=16324&y=10894&z=15
I/flutter (14390): Fetching image from: http://mt.google.com/vt/lyrs=m&x=16325&y=10894&z=15
I/flutter (14390): Saving image to file: /data/user/0/[appname]/app_flutter/mt.google.com/vt/lyrs=m&x=16324&y=10894&z=15
I/flutter (14390): Saving image to file: /data/user/0/[appname]/app_flutter/mt.google.com/vt/lyrs=m&x=16325&y=10894&z=15
etc...

, which means the downloading of images are working as intended.

However, once I go offline (there is no special mode yet, but I think the network_to_file_image package is supposed to work like this...), the images still try to be fetched from the internet, resulting in a SocketException error.

Has anyone else implemented a similar logic? I understand that this package will cache these tiles, but anything in the cache is vulnerable to the system clearing it, and in my circumstance, this would be quite a large problem.

Thank you very much!
Luka S.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions