Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)<br>
Once you have your map exported to `.mbtiles`, you can use [mbtilesToPng](https://github.com/alfanhui/mbtilesToPngs) to unpack into `/{z}/{x}/{y}.png`.
Expand Down
6 changes: 6 additions & 0 deletions lib/src/layer/tile_layer.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -118,6 +119,10 @@ class TileLayerOptions extends LayerOptions {
/// TileProvider
///
final TileProvider tileProvider;

// If using PersistentAdvancedCacheTileProvider()
// Set this to: `await getApplicationDocumentsDirectory()`
final Directory saveDir;

/// When panning the map, keep this many rows and columns of tiles before
/// unloading them.
Expand Down Expand Up @@ -206,6 +211,7 @@ class TileLayerOptions extends LayerOptions {
this.placeholderImage,
this.errorImage,
this.tileProvider = const CachedNetworkTileProvider(),
this.saveDir,
this.tms = false,
// ignore: avoid_init_to_null
this.wmsOptions = null,
Expand Down
25 changes: 25 additions & 0 deletions lib/src/layer/tile_provider/tile_provider.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -96,6 +98,29 @@ class FileTileProvider extends TileProvider {
}
}

class PersistentAdvancedCacheTileProvider extends TileProvider {
// Created by JaffaKetchup
@override
ImageProvider getImage(Coords<num> coords, TileLayerOptions options) {
final Directory _appDocDirFolder =
Directory(options.saveDir.path + '/tiles/');
if (!_appDocDirFolder.existsSync()) {
_appDocDirFolder.createSync(recursive: true);
}
return NetworkToFileImage(
url: getTileUrl(coords, options),
file: File(
p.join(
options.saveDir.path + '/tiles/',
getTileUrl(coords, options)
.replaceAll('https://', '')
.replaceAll('http://', '')
.replaceAll("/", "")),
),
);
}
}

class CustomTileProvider extends TileProvider {
String Function(Coords coors, TileLayerOptions options) customTileUrl;

Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down