Dioxus Leaflet
A general-purpose Leaflet map component for Dioxus applications. This crate provides an easy-to-use, reactive map component that integrates seamlessly with Dioxus applications.
Features
- Component-based map objects - intuitive Dioxus components for markers, polygons, and popups
- Interactive markers with popups and custom styling
- Reactive map components that auto-update when your data changes
- Flexible Leaflet integration - CDN with version selection or local files
- Configurable Leaflet resources with integrity checking for security
- Responsive design with customizable dimensions
- Customizable tile layers including OpenStreetMap and satellite imagery
- Configurable map options for zoom, dragging, and interaction controls
- Event handling for map clicks and movements
Screenshots


Also works on mobile !


Installation
Add this to your Cargo.toml:
[]
= "0.2.0"
= "0.7.0-rc.0"
Quick Start
Here's a simple example to get you started:
use *;
use ;
Core Components
Map Component
The main Map component provides a full-featured Leaflet map. It can contain child components like Marker and Polygon:
rsx!
Map Properties
| Property | Type | Default | Description |
|---|---|---|---|
initial_position |
MapPosition |
London coordinates | Initial map center and zoom |
markers |
Vec<MapMarker> |
Empty | Markers to display on the map |
height |
String |
"500px" |
Height of the map container |
width |
String |
"100%" |
Width of the map container |
options |
MapOptions |
Default | Map configuration options |
class |
String |
"" |
Additional CSS classes |
style |
String |
"" |
Additional CSS styles |
on_marker_click |
EventHandler<MapMarker> |
None | Callback when marker is clicked |
on_map_click |
EventHandler<MapPosition> |
None | Callback when map is clicked |
on_map_move |
EventHandler<MapPosition> |
None | Callback when map is moved |
Leaflet Resources Configuration
Configure how Leaflet CSS and JavaScript files are loaded. You can use CDN with specific versions or provide local files.
Using CDN with Default Version (1.9.4)
// Default configuration uses Leaflet 1.9.4 from unpkg.com
let options = default;
Using CDN with Specific Version
use ;
let options = default
.with_leaflet_resources;
Using Custom CDN Base URL
let options = default
.with_leaflet_resources;
Using Local Files
let options = default
.with_leaflet_resources;
Leaflet Resource Options
| Method | Description | Security |
|---|---|---|
LeafletResources::cdn(version) |
Uses unpkg.com CDN with specified version | Includes integrity checking for known versions |
LeafletResources::cdn_with_base_url(version, base_url) |
Uses custom CDN base URL | No integrity checking for custom URLs |
LeafletResources::local(css_path, js_path) |
Uses local files from specified paths | No integrity checking |
Note: Integrity checking is automatically applied for known Leaflet versions (1.9.2, 1.9.3, 1.9.4) when using the default unpkg.com CDN.
Working with Markers
Basic Markers
let marker = new;
Advanced Markers
let marker = new
.with_description
.with_custom_data
.with_custom_data
.with_popup_options;
Custom Marker Icons
use MarkerIcon;
let custom_icon = new
.with_size
.with_anchor;
let marker = new
.with_icon;
Map Configuration
Map Options
Customize map behavior with MapOptions. All options have sensible defaults:
use ;
// Full configuration with all options specified
let options = MapOptions ;
// Minimal configuration using builder pattern
let minimal_options = default
.with_double_click_zoom
.with_tile_layer;
// All controls disabled
let disabled_options = minimal
.with_tile_layer;
// Use default configuration
let default_options = default; // All options set to sensible defaults
rsx!
Available Map Options
| Option | Type | Default | Description |
|---|---|---|---|
zoom_control |
bool |
true |
Show/hide zoom control buttons |
scroll_wheel_zoom |
bool |
true |
Enable/disable scroll wheel zooming |
double_click_zoom |
bool |
true |
Enable/disable double-click zooming |
touch_zoom |
bool |
true |
Enable/disable touch/pinch zooming |
dragging |
bool |
true |
Enable/disable map dragging |
keyboard |
bool |
true |
Enable/disable keyboard navigation |
attribution_control |
bool |
true |
Show/hide attribution control |
tile_layer |
TileLayer |
OpenStreetMap | Tile layer configuration |
leaflet_resources |
LeafletResources |
CDN v1.9.4 | Leaflet CSS/JS resource configuration |
Tile Layers
Choose from different tile layer providers:
use TileLayer;
// OpenStreetMap (default)
let osm_tiles = openstreetmap;
// Satellite imagery
let satellite_tiles = satellite;
// Custom tile layer
let custom_tiles = TileLayer ;
Event Handling
Handle various map and marker events:
Styling
CSS Classes
The component uses these CSS classes that you can style:
.dioxus-leaflet-container- Main container.dioxus-leaflet-map- Map element
Custom Styling
}
}
Examples
Basic Map with Custom Leaflet Version
use *;
use ;
Map with Local Leaflet Files
use *;
use ;
Tourist Map
use *;
use ;
Real-time Location Tracking
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or Apache 2.0)
- MIT License (LICENSE-MIT or MIT)
at your option.
Acknowledgments
- Leaflet - The amazing mapping library this component wraps
- Dioxus - The reactive UI library for Rust
- OpenStreetMap - Free geographic data used in examples
Note:
Internet Connection Requirements
- CDN mode: Requires internet connection to load Leaflet from CDN (default behavior)
- Local mode: Works offline when using local Leaflet files
Setting Up Local Files
To use local Leaflet files:
- Download Leaflet from leafletjs.com
- Place the CSS and JS files in your static assets directory
- Configure the paths using
LeafletResources::local()
Example directory structure:
static/
├── css/
│ └── leaflet.css
├── js/
│ └── leaflet.js
Usage:
let options = default
.with_leaflet_resources;