improved overhead
This commit is contained in:
parent
d0d9783b78
commit
615af106d8
2 changed files with 47 additions and 31 deletions
|
@ -54,50 +54,66 @@ class OsmLibertyLayer extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _OsmLibertyLayerState extends State<OsmLibertyLayer> {
|
class _OsmLibertyLayerState extends State<OsmLibertyLayer> {
|
||||||
late final Future<Style> _americanaStyleFuture;
|
late final Future<TileProviders> _tileProviderFuture;
|
||||||
late final Future<Style> _osmLibertyStyleFuture;
|
late final Future<Style> _styleFuture;
|
||||||
|
|
||||||
static const _openMapTileProviderSource = 'openmaptiles';
|
static const _openMapTileProviderSource = 'openmaptiles';
|
||||||
|
|
||||||
// `Americana` provides tiles, but it uses layer syntax that is not supported by the vector tile renderer
|
// `Americana` provides tiles, but it uses layer syntax that is not supported by the vector tile renderer.
|
||||||
static const _americanaStyle = 'https://americanamap.org/style.json';
|
// Full style is at 'https://americanamap.org/style.json' but it is heavy (1.0 MB, mostly for the layers).
|
||||||
|
static const _americanaTileProviderUri = 'https://tile.ourmap.us/data/v3.json';
|
||||||
|
|
||||||
// `OSM Liberty` is well supported by the vector tile renderer, but it requires an API key for the tiles
|
// `OSM Liberty` is well supported by the vector tile renderer, but it requires an API key for the tiles.
|
||||||
static const _osmLiberty = 'https://maputnik.github.io/osm-liberty/style.json';
|
static const _osmLibertyStyleUri = 'https://maputnik.github.io/osm-liberty/style.json';
|
||||||
|
|
||||||
|
// as of 2024/09/25,
|
||||||
|
// Americana provider JSON: 39.4 kB
|
||||||
|
// OSM Liberty style JSON: 48.3 kB
|
||||||
|
// OSM Liberty sprites JSON 1x: 16.6 kB
|
||||||
|
// OSM Liberty sprites PNG 1x: 30.4 kB
|
||||||
|
// OSM Liberty sprites JSON 2x: 16.6 kB
|
||||||
|
// OSM Liberty sprites PNG 2x: 82.5 kB
|
||||||
|
// -> total overhead: 233.8 kB
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
_americanaStyleFuture = StyleReader(
|
_tileProviderFuture = StyleReaderExtra.readProviderByName(
|
||||||
uri: _americanaStyle,
|
{
|
||||||
).readExtra(skippedSources: {});
|
_openMapTileProviderSource: {
|
||||||
|
'url': _americanaTileProviderUri,
|
||||||
|
'type': 'vector',
|
||||||
|
}
|
||||||
|
},
|
||||||
|
).then(TileProviders.new);
|
||||||
|
|
||||||
_osmLibertyStyleFuture = StyleReader(
|
_styleFuture = StyleReader(
|
||||||
uri: _osmLiberty,
|
uri: _osmLibertyStyleUri,
|
||||||
logger: const vtr.Logger.console(),
|
logger: const vtr.Logger.console(),
|
||||||
).readExtra(skippedSources: {_openMapTileProviderSource});
|
).readExtra(skipSources: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return FutureBuilder<Style>(
|
return FutureBuilder<TileProviders>(
|
||||||
future: _americanaStyleFuture,
|
future: _tileProviderFuture,
|
||||||
builder: (context, americanaStyleSnapshot) {
|
builder: (context, tileProviderSnapshot) {
|
||||||
return FutureBuilder<Style>(
|
return FutureBuilder<Style>(
|
||||||
future: _osmLibertyStyleFuture,
|
future: _styleFuture,
|
||||||
builder: (context, osmLibertyStyleSnapshot) {
|
builder: (context, styleSnapshot) {
|
||||||
if (americanaStyleSnapshot.hasError) return Text(americanaStyleSnapshot.error.toString());
|
if (tileProviderSnapshot.hasError) return Text(tileProviderSnapshot.error.toString());
|
||||||
if (osmLibertyStyleSnapshot.hasError) return Text(osmLibertyStyleSnapshot.error.toString());
|
if (styleSnapshot.hasError) return Text(styleSnapshot.error.toString());
|
||||||
|
|
||||||
final americanaStyle = americanaStyleSnapshot.data;
|
final tileProviders = tileProviderSnapshot.data;
|
||||||
final osmLibertyStyle = osmLibertyStyleSnapshot.data;
|
final style = styleSnapshot.data;
|
||||||
if (americanaStyle == null || osmLibertyStyle == null) return const SizedBox();
|
if (tileProviders == null || style == null) return const SizedBox();
|
||||||
|
|
||||||
return VectorTileLayer(
|
return VectorTileLayer(
|
||||||
tileProviders: americanaStyle.providers,
|
tileProviders: tileProviders,
|
||||||
theme: osmLibertyStyle.theme,
|
theme: style.theme,
|
||||||
sprites: osmLibertyStyle.sprites,
|
sprites: style.sprites,
|
||||||
|
// `vector` is higher quality and follows map orientation, but it is slower
|
||||||
layerMode: VectorTileLayerMode.raster,
|
layerMode: VectorTileLayerMode.raster,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
|
@ -7,14 +7,14 @@ import 'package:vector_map_tiles/vector_map_tiles.dart';
|
||||||
import 'package:vector_tile_renderer/vector_tile_renderer.dart';
|
import 'package:vector_tile_renderer/vector_tile_renderer.dart';
|
||||||
|
|
||||||
extension StyleReaderExtra on StyleReader {
|
extension StyleReaderExtra on StyleReader {
|
||||||
Future<Style> readExtra({required Set<String> skippedSources}) async {
|
Future<Style> readExtra({required bool skipSources}) async {
|
||||||
final styleText = await _httpGet(uri);
|
final styleText = await _httpGet(uri);
|
||||||
final style = await compute(jsonDecode, styleText);
|
final style = await compute(jsonDecode, styleText);
|
||||||
if (style is! Map<String, dynamic>) {
|
if (style is! Map<String, dynamic>) {
|
||||||
throw _invalidStyle(uri);
|
throw _invalidStyle(uri);
|
||||||
}
|
}
|
||||||
final sources = style['sources'] as Map<String, dynamic>;
|
final sources = style['sources'] as Map<String, dynamic>;
|
||||||
final providerByName = await _readProviderByName(Map.fromEntries(sources.entries.where((kv) => !skippedSources.contains(kv.key))));
|
final Map<String, VectorTileProvider> providerByName = skipSources ? {} : await readProviderByName(sources);
|
||||||
final name = style['name'] as String?;
|
final name = style['name'] as String?;
|
||||||
|
|
||||||
final center = style['center'];
|
final center = style['center'];
|
||||||
|
@ -52,7 +52,7 @@ extension StyleReaderExtra on StyleReader {
|
||||||
return Style(theme: ThemeReader(logger: logger).read(style), providers: TileProviders(providerByName), sprites: sprites, name: name, center: centerPoint, zoom: zoom);
|
return Style(theme: ThemeReader(logger: logger).read(style), providers: TileProviders(providerByName), sprites: sprites, name: name, center: centerPoint, zoom: zoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, VectorTileProvider>> _readProviderByName(Map<String, dynamic> sources) async {
|
static Future<Map<String, VectorTileProvider>> readProviderByName(Map<String, dynamic> sources) async {
|
||||||
final providers = <String, VectorTileProvider>{};
|
final providers = <String, VectorTileProvider>{};
|
||||||
final sourceEntries = sources.entries.toList();
|
final sourceEntries = sources.entries.toList();
|
||||||
for (final entry in sourceEntries) {
|
for (final entry in sourceEntries) {
|
||||||
|
@ -84,9 +84,9 @@ extension StyleReaderExtra on StyleReader {
|
||||||
return providers;
|
return providers;
|
||||||
}
|
}
|
||||||
|
|
||||||
String _invalidStyle(String url) => 'Uri does not appear to be a valid style: $url';
|
static String _invalidStyle(String url) => 'Uri does not appear to be a valid style: $url';
|
||||||
|
|
||||||
Future<String> _httpGet(String url) async {
|
static Future<String> _httpGet(String url) async {
|
||||||
final response = await get(Uri.parse(url));
|
final response = await get(Uri.parse(url));
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
return response.body;
|
return response.body;
|
||||||
|
@ -95,7 +95,7 @@ extension StyleReaderExtra on StyleReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Uint8List> _loadBinary(String url) async {
|
static Future<Uint8List> _loadBinary(String url) async {
|
||||||
final response = await get(Uri.parse(url));
|
final response = await get(Uri.parse(url));
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
return response.bodyBytes;
|
return response.bodyBytes;
|
||||||
|
|
Loading…
Reference in a new issue