Offline PMTiles Raster Maps for Flutter Map

Resumo

Pixel shares an overview of pmtiles_provider, a Flutter Map tile provider for raster PMTiles v3 archives. It explains loading a single map file from storage, assets, memory, or static hosts, plus HTTP range requests, caching, archive change detection, and metadata-based map options.

DEV Community

pmtiles_provider: offline raster maps in flutter_map from one PMTiles file

An offline map usually means a folder with thousands of small tile images, or a tile server you have to run. PMTiles is a simpler idea: the whole map is one file, with an index at the start that says where each tile sits. A reader needs only the header, a few directories and the tiles on screen. The same file can sit on a phone's storage or on a plain static host, where the reader fetches byte ranges over HTTP. pmtiles_provider brings that to flutter_map. It is a TileProvider for raster PMTiles v3 archives. You can show a single-file map offline from a file, an asset or memory, or stream it from any static host with HTTP range requests. One thing to say up front: it draws raster tiles (PNG, JPEG, WebP). It is not a vector tile renderer. Opening an MVT archive fails with a clear error that points you to a vector renderer. What it does Four sources. PmTilesProvider.fromFile, .fromBytes, .fromAsset and .fromUrl. On files, access is random: only the header, the directories and the visible tiles are read. File access sits behind a conditional import, so the package still compiles for the web. HTTP range requests with change detection. fromUrl sends Range requests and tracks the ETag, Last-Modified and size of the archive. If the archive is replaced on the server in the middle of a session, the change is detected, the caches are dropped, the request is retried, and provider.changes fires so TileLayer.reset can reload the stale tiles. Caches. There is an LRU cache for leaf directories (by count) and one for tile bytes (by size). Both sizes are configurable. The header and the root directory come from a single 16 KiB read. Metadata. provider.info() gives the bounds, center, min and max zoom, tile type, name and attribution of the archive. info.mapOptions() builds MapOptions from it, so the map opens on the right place. Missing tiles. A missing tile can be transparent, an error (so TileLayer.errorImage shows), or overzoomed: the nearest ancestor tile is cropped and scaled up. Overzoom also fills zooms deeper than the archive's max zoom. Cancellation. A tile that leaves the screen aborts its pending range read. Reads shared by several tiles are aborted only when every waiting tile has cancelled. A reader written from the spec. The PMTiles v3 reader covers the header, varint and delta directories, run lengths, leaf directories, Hilbert TileIDs and gzip. How to use it Add it from GitHub, next to flutter_map and latlong2: dependencies:
flutter_map: ^8.0.0
latlong2: ^0.9.1
pmtiles_provider:
git: https://github.com/AbdeldjalilChougui/pmtiles_provider
Then give the provider to a TileLayer: import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:pmtiles_provider/pmtiles_provider.dart';

class OfflineMap extends StatefulWidget {
const OfflineMap({super.key, required this.path});
final String path;

@override
State createState() => _OfflineMapState();
}

class _OfflineMapState extends State {
late final provider = PmTilesProvider.fromFile(
widget.path,
missingTile: const MissingTileBehavior.overzoom(),
);
late final info = provider.info();

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: info,
builder: (context, snapshot) {
final info = snapshot.data;
if (info == null) return const SizedBox();
return FlutterMap(
options: info.mapOptions(), // center, zoom, bounds from the archive
children: [
TileLayer(
tileProvider: provider,
reset: provider.changes,
userAgentPackageName: 'com.example.app',
),
],
);
},
);
}
}
The other sources look like this: PmTilesProvider.fromUrl('https://cdn.example.com/region.pmtiles');
PmTilesProvider.fromAsset('assets/region.pmtiles'); // loaded into memory
PmTilesProvider.fromBytes(bytes);
Every constructor accepts...
Offline PMTiles Raster Maps for Flutter Map
Powered by Readybot.io

As últimas novidades de Pixel

Stop Paying for AI Renders in Test Orders

Pixel’s announcement explains how internal test orders were accidentally triggering paid AI video renders costing just over $1 each. It details a server-side dry-run guard …

Sandblasting Grit Guide: 120 vs 220 vs 320

Pixel’s announcement explains how 120, 220, and 320 grit affect aluminium’s matte finish and reveal die-cast porosity. It also covers how pressure, blast distance, and …

NVIDIA Cosmos 3 Edge: Physical AI on Jetson

Pixel shares the first article in CAVEDU's Cosmos series, introducing NVIDIA Cosmos 3 Edge, an approximately 4-billion-parameter world model for Jetson devices. It explores multimodal …

Laravel Semantic Search with OpenAI and Pinecone

Pixel shares a practical guide to building semantic search in Laravel using OpenAI embeddings and Pinecone. It covers 1536-dimensional vectors, cosine similarity, index setup, environment …