flutter_map Documentation
repository·master·Indexed 25 days ago
https://github.com/fleaflet/flutter_mapA pure-Flutter, vendor-free, and cross-platform map client. It provides tools for displaying raster map tiles via TileLayer, drawing shapes with PolygonLayer, and overlaying images using OverlayImage and RotatedOverlayImage. The library includes a built-in map caching provider (BuiltInMapCachingProvider) with a specific tile file format (v1) and size monitoring system.
What's inside flutter_map
- flutter_map is a non-commercially aimed, versatile, and vendor-free map client for Flutter. It is fully cross-platform and built using 100% pure Flutter code.
Understand the custom binary GeoJSON format for stress testing
masterFor high-performance polygon stress testing (involving ~138k vertices),
flutter_mapuses a custom binary format instead of raw GeoJSON. This format is designed to optimize asset size and unpacking speed while removing unnecessary metadata like names or CRS.Key characteristics of the binary format:
- Size Efficiency: The binary file is approximately 6x smaller than the original GeoJSON.
- Unpacking Efficiency: The format is optimized for fast parsing/unpacking over compression (like RLE).
- Precision: Coordinate components are scaled and stored as 4-byte integers. This results in negligible geographical precision loss.
- Implementation: The format is non-streaming and is tailored specifically to the geometry data provided in the stress test sample.
- Packing Logic: The packing algorithm is implemented in
pack.dart. It scales decimal coordinates to integers and stores the byte length for each polygon before its coordinate data.
Understand BuiltInMapCachingProvider storage specification
masterTheBuiltInMapCachingProvider(built-in caching) uses the native platform's filesystem for storage. It stores cached tiles and metadata as individual keyed files. A special file namedsizeMonitor.binis used to track the total cache size efficiently without performing expensive I/O operations on every startup.Run the flutter_map demo application
masterTo run the demo application to explore
flutter_mapfeatures, clone the repository to your local machine, connect a device or emulator, and execute the following commands in your terminal:flutter cleanflutter run
Note that there are no pre-built versions available; you must run it from the source.
flutter clean flutter runCustomize iOS Launch Screen Assets
masterTo customize the iOS launch screen, you can either replace the image files directly in the
example/ios/Runner/Assets.xcassets/LaunchImage.imageset/directory or use Xcode.To use Xcode:
- Open the iOS project using
open ios/Runner.xcworkspace. - In the Xcode Project Navigator, select
Runner/Assets.xcassets. - Drag and drop your desired images into the asset catalog.
open ios/Runner.xcworkspace- Open the iOS project using
Configure TileLayer URL templates and options
masterConfigure how tiles are fetched using
urlTemplateandadditionalOptions.additionalOptionsallows you to inject static values (like API keys) into the URL template placeholders.Key Parameters:
urlTemplate: A string containing placeholders like{z},{x},{y}, and{s}(subdomain).additionalOptions: A map of key-value pairs to replace placeholders in theurlTemplate.subdomains: A list of subdomains (e.g.,['a', 'b', 'c']) to be used with the{s}placeholder.fallbackUrl: A fallback template used if the primaryurlTemplatefails. Note: using this disables in-memory caching to prevent mixed tilesets.
Fine-tune multi-finger gesture competition
masterWhen
enableMultiFingerGestureRaceis set totrue, multiple gestures can compete to become the active interaction. If multiple gestures reach their thresholds simultaneously, precedence is given in this order:pinchZoomWinGestures>rotationWinGestures>pinchMoveWinGestures.To adjust this behavior, use:
rotationThreshold: Degrees required to start rotation (default:20.0).pinchZoomThreshold: Threshold to start zooming (default:0.5).pinchMoveThreshold: Threshold to start moving via pinch (default:40.0).rotationWinGestures: TheMultiFingerGesturebitmask used when rotation wins.pinchZoomWinGestures: TheMultiFingerGesturebitmask used when zoom wins.pinchMoveWinGestures: TheMultiFingerGesturebitmask used when pinch-move wins.
Note: These only take effect if the corresponding
InteractiveFlagis present inflags.Configure abortObsoleteRequests in NetworkTileProvider
masterThe
abortObsoleteRequestsoption allowsNetworkTileProviderto cancel in-flight HTTP requests for tiles that are no longer being displayed (e.g., when a user zooms quickly past a certain level).Benefits:
- Improved tile loading speeds.
- Reduced network data consumption.
- Reduced storage usage in the
cachingProvider. - Reduced tile server costs.
Note: This functionality is most effective on web platforms (using
BrowserClient) or with clients/servers that have limited simultaneous connections. If the providedhttpClientdoes not support standard request aborting, this option will have no effect. It is recommended to keep this enabled unless issues arise.Tile file format (v1) specification
masterTiles are stored as files where the filename is the output of the
cacheKeyGenerator(defaulting to a v5 UUID). The file contains a metadata header followed by the raw tile image bytes.Header Structure:
- Identifier (6 bytes): ASCII string
FMBICT. - Version (2 bytes): Uint16 (currently
1). - staleAt (8 bytes): Int64 timestamp (milliseconds since Unix epoch, UTC).
- lastModified (8 bytes): Int64 timestamp (milliseconds since Unix epoch, UTC). If not provided, use
0. - etag Length (2 bytes): Uint16 representing the length of the ASCII encoded
etag. - etag (Variable): ASCII encoded string (max 65535 bytes). If not provided, 0 bytes.
- Image Length (4 bytes): Uint32 representing the length of the tile image bytes following the header.
- Identifier (6 bytes): ASCII string
Size monitor specification
masterThe size monitor is stored in a file named
sizeMonitor.bin. It contains a single 8-byte unsigned integer (Uint64) representing the total size of all tiles and metadata in the cache in bytes.Important behaviors:
- Synchronization: The monitor must stay in sync with the actual filesystem size. If a read failure occurs (suggesting corruption), the monitor must be disabled and recalculated on the next startup.
- Recalculation: Recalculation is an expensive I/O operation performed on startup. While calculating, writes must be delayed, but reads can still occur.
Handle Web limitations with NetworkTileProvider
masterWhen using
NetworkTileProvideron the web, be aware of the following limitations:- User-Agent Header: The
User-Agentheader cannot be modified due to Dart/browser limitations. - Request Cancellation: While
supportsCancelLoadingreturnstrue, actual abortion of in-flight HTTP requests is not yet supported in Dart on the web. This meansgetImageWithCancelLoadingSupportcannot fully abort requests on web platforms.
- User-Agent Header: The
Apply a CameraConstraint to a MapCamera
masterTo apply a constraint to a camera, call the
constrain(MapCamera camera)method on aCameraConstraintinstance.Note: The method returns a nullable
MapCamera?. It returnsnullif no appropriate camera could be generated by the movement (for example, if the camera is zoomed out too far to satisfy the constraint).