From fbcffce41ef04489e9cc92814b29884fa18d7f56 Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 18:18:43 +0000 Subject: [PATCH 01/11] Added More Persistent Tile Downloader For Caching --- .../layer/tile_provider/tile_provider.dart | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/src/layer/tile_provider/tile_provider.dart b/lib/src/layer/tile_provider/tile_provider.dart index be9bac443..c76b39674 100644 --- a/lib/src/layer/tile_provider/tile_provider.dart +++ b/lib/src/layer/tile_provider/tile_provider.dart @@ -1,5 +1,7 @@ import 'dart:io'; +import 'package:path/path.dart' as p; +import 'package:network_to_file_image/network_to_file_image.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_image/network.dart'; @@ -96,6 +98,29 @@ class FileTileProvider extends TileProvider { } } +class PersistentAdvancedCacheTileProvider extends TileProvider { + // Created by JaffaKetchup + @override + ImageProvider getImage(Coords coords, TileLayerOptions options) { + final Directory _appDocDirFolder = + Directory(globals.saveDir.path + '/tiles/'); + if (!_appDocDirFolder.existsSync()) { + _appDocDirFolder.createSync(recursive: true); + } + return NetworkToFileImage( + url: getTileUrl(coords, options), + file: File( + p.join( + globals.saveDir.path + '/tiles/', + getTileUrl(coords, options) + .replaceAll('https://', '') + .replaceAll('http://', '') + .replaceAll("/", "")), + ), + ); + } +} + class CustomTileProvider extends TileProvider { String Function(Coords coors, TileLayerOptions options) customTileUrl; From ef29ff94604f7c03bcc234287e54617a2af32673 Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 18:21:06 +0000 Subject: [PATCH 02/11] Added network_to_file_image For addition of tile provider --- pubspec.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/pubspec.yaml b/pubspec.yaml index fa8335b10..35ad45cd2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,6 +23,7 @@ dependencies: vector_math: ^2.0.0 proj4dart: ^1.0.4 meta: ^1.1.0 + network_to_file_image: ^2.3.6 dev_dependencies: pedantic: ^1.8.0 From 5089879241ecccf93940913751eac0d7518dd272 Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 18:32:56 +0000 Subject: [PATCH 03/11] Update tile_provider.dart --- lib/src/layer/tile_provider/tile_provider.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/layer/tile_provider/tile_provider.dart b/lib/src/layer/tile_provider/tile_provider.dart index c76b39674..34b3bb183 100644 --- a/lib/src/layer/tile_provider/tile_provider.dart +++ b/lib/src/layer/tile_provider/tile_provider.dart @@ -103,7 +103,7 @@ class PersistentAdvancedCacheTileProvider extends TileProvider { @override ImageProvider getImage(Coords coords, TileLayerOptions options) { final Directory _appDocDirFolder = - Directory(globals.saveDir.path + '/tiles/'); + Directory(options.appDir + '/tiles/'); if (!_appDocDirFolder.existsSync()) { _appDocDirFolder.createSync(recursive: true); } @@ -111,7 +111,7 @@ class PersistentAdvancedCacheTileProvider extends TileProvider { url: getTileUrl(coords, options), file: File( p.join( - globals.saveDir.path + '/tiles/', + options.appDir + '/tiles/', getTileUrl(coords, options) .replaceAll('https://', '') .replaceAll('http://', '') From 04e99150439337e261cab7152a5e84d1a3d23f73 Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 18:38:22 +0000 Subject: [PATCH 04/11] Added 'saveDir' option --- lib/src/layer/tile_layer.dart | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/src/layer/tile_layer.dart b/lib/src/layer/tile_layer.dart index 9739a4ec6..f692230f4 100644 --- a/lib/src/layer/tile_layer.dart +++ b/lib/src/layer/tile_layer.dart @@ -118,6 +118,11 @@ class TileLayerOptions extends LayerOptions { /// TileProvider /// final TileProvider tileProvider; + + // If using PersistentAdvancedCacheTileProvider() + // First set `final Directory saveDir = await getApplicationDocumentsDirectory();` + // Then pass to this option `saveDir.path` + final Directory saveDir; /// When panning the map, keep this many rows and columns of tiles before /// unloading them. @@ -206,6 +211,7 @@ class TileLayerOptions extends LayerOptions { this.placeholderImage, this.errorImage, this.tileProvider = const CachedNetworkTileProvider(), + this.saveDir = false, this.tms = false, // ignore: avoid_init_to_null this.wmsOptions = null, From 45cbda178436c8c183069b911d7cffccdb577df0 Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 19:35:28 +0000 Subject: [PATCH 05/11] Import dart:io To fix ''Directory' isn't a type' build error. --- lib/src/layer/tile_layer.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/src/layer/tile_layer.dart b/lib/src/layer/tile_layer.dart index f692230f4..5d37bd753 100644 --- a/lib/src/layer/tile_layer.dart +++ b/lib/src/layer/tile_layer.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:math' as math; +import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; From 82425c3d2da21e7b7f82d16b16dfb0ceae944872 Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 19:38:23 +0000 Subject: [PATCH 06/11] Update options --- lib/src/layer/tile_layer.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/src/layer/tile_layer.dart b/lib/src/layer/tile_layer.dart index 5d37bd753..f1e08c1eb 100644 --- a/lib/src/layer/tile_layer.dart +++ b/lib/src/layer/tile_layer.dart @@ -121,8 +121,7 @@ class TileLayerOptions extends LayerOptions { final TileProvider tileProvider; // If using PersistentAdvancedCacheTileProvider() - // First set `final Directory saveDir = await getApplicationDocumentsDirectory();` - // Then pass to this option `saveDir.path` + // Set this to: `await getApplicationDocumentsDirectory()` final Directory saveDir; /// When panning the map, keep this many rows and columns of tiles before From 7964dc5c9af7ad375fc8c945736fe9317a3d3ecc Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 19:40:06 +0000 Subject: [PATCH 07/11] Fix options.appDir To fix ''appDir' isn't defined for the class 'TileLayerOptions''. --- lib/src/layer/tile_provider/tile_provider.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/layer/tile_provider/tile_provider.dart b/lib/src/layer/tile_provider/tile_provider.dart index 34b3bb183..ca4b353be 100644 --- a/lib/src/layer/tile_provider/tile_provider.dart +++ b/lib/src/layer/tile_provider/tile_provider.dart @@ -103,7 +103,7 @@ class PersistentAdvancedCacheTileProvider extends TileProvider { @override ImageProvider getImage(Coords coords, TileLayerOptions options) { final Directory _appDocDirFolder = - Directory(options.appDir + '/tiles/'); + Directory(options.saveDir.path + '/tiles/'); if (!_appDocDirFolder.existsSync()) { _appDocDirFolder.createSync(recursive: true); } @@ -111,7 +111,7 @@ class PersistentAdvancedCacheTileProvider extends TileProvider { url: getTileUrl(coords, options), file: File( p.join( - options.appDir + '/tiles/', + options.saveDir.path + '/tiles/', getTileUrl(coords, options) .replaceAll('https://', '') .replaceAll('http://', '') From 9c532632c5cb3388b613ce5f2fc2b767c38f5e57 Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 19:42:41 +0000 Subject: [PATCH 08/11] Remove bool default assignment to saveDir To fix 'A value of type 'bool' can't be assigned to a variable of type 'Directory'' --- lib/src/layer/tile_layer.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/layer/tile_layer.dart b/lib/src/layer/tile_layer.dart index f1e08c1eb..436cf0a37 100644 --- a/lib/src/layer/tile_layer.dart +++ b/lib/src/layer/tile_layer.dart @@ -211,7 +211,7 @@ class TileLayerOptions extends LayerOptions { this.placeholderImage, this.errorImage, this.tileProvider = const CachedNetworkTileProvider(), - this.saveDir = false, + this.saveDir, this.tms = false, // ignore: avoid_init_to_null this.wmsOptions = null, From fc05173e79a3ea98c2b598e943a3fa322c8bb406 Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 20:01:23 +0000 Subject: [PATCH 09/11] Update README.md --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 3a151c700..0e0b5204c 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,17 @@ Then execute `ulimit -S -n 2048` ([ref](https://github.com/trentpiercy/trace/iss Then execute `flutter run` with a running emulator. ## Offline maps +Full offline map functionality has been created by JaffaKetchup. Use `PersistentAdvancedCacheTileProvider()` as the `TileLayerOptions > tileProvider` option, and set `TileLayerOptions > saveDir` to a variable containing (await) `getApplicationDocumentsDirectory()` from library [`path_provider`](https://pub.dev/packages/path_provider). + +As the user scrolls around the map, tiles will be saved to `[saveDir]/tiles`. Then, when requested again, tiles will be taken from that directory if available, else a network tile request will be made. Thus, this is a file first solution. + +This configuration should be used where offline functionality is required because cached tiles from other tile providers can be cleaned by the system without user notification at any point. With this setup, only the user (through App Settings > Storage > Clear Storage) or the app itself can clear the tiles. It is up to the app developer (you) to manage these tiles... + +If your user wants to get the latest tiles from the server for tiles already loaded, you must clear the tiles folder. If the user wishes to remove tiles altogether, you must provide that functionality. This library will not handle that for you. + +A recommended (however untested) integration is to use another tile provider (such as the default) whilst not in 'Offline Mode' so that the newest tiles are always loaded, then switch to this provider once the user enters 'Download & Offline Mode'. Then, when the user switches back out, clear the tiles folder. + +## Preconfigured maps (previously 'Offline maps') [Follow this guide to grab offline tiles](https://tilemill-project.github.io/tilemill/docs/guides/osm-bright-mac-quickstart/)
Once you have your map exported to `.mbtiles`, you can use [mbtilesToPng](https://github.com/alfanhui/mbtilesToPngs) to unpack into `/{z}/{x}/{y}.png`. From 4f15e625eaa977120b2c5c6d7cddc5a19e3f6d26 Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 20:23:05 +0000 Subject: [PATCH 10/11] Added 'path' library requirement --- pubspec.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/pubspec.yaml b/pubspec.yaml index 35ad45cd2..153377717 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -24,6 +24,7 @@ dependencies: proj4dart: ^1.0.4 meta: ^1.1.0 network_to_file_image: ^2.3.6 + path: ^1.7.0 dev_dependencies: pedantic: ^1.8.0 From 1fac67954e15da8df2696bed706e5696269e0255 Mon Sep 17 00:00:00 2001 From: Luka S <58115698+JaffaKetchup@users.noreply.github.com> Date: Wed, 6 Jan 2021 20:26:31 +0000 Subject: [PATCH 11/11] Removal of 'path' to fix build --- pubspec.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 153377717..35ad45cd2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -24,7 +24,6 @@ dependencies: proj4dart: ^1.0.4 meta: ^1.1.0 network_to_file_image: ^2.3.6 - path: ^1.7.0 dev_dependencies: pedantic: ^1.8.0