Skip to main content

dioxus_leaflet/types/
map_options.rs

1use super::{LeafletResources, TileLayer};
2use serde::{Deserialize, Serialize};
3
4/// Coordinate reference system, matching the names of Leaflet's `L.CRS.*` objects
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub enum CRS {
7    /// World Mercator (`L.CRS.EPSG3395`)
8    EPSG3395,
9    /// Spherical Mercator, the default used by most tile providers (`L.CRS.EPSG3857`)
10    EPSG3857,
11    /// Plate Carrée (`L.CRS.EPSG4326`)
12    EPSG4326,
13    /// Alias of EPSG3857 (`L.CRS.EPSG900913`)
14    EPSG900913,
15    /// Earth CRS (`L.CRS.Earth`)
16    Earth,
17    /// Simple linear CRS for non-geographical maps, such as game maps (`L.CRS.Simple`)
18    Simple,
19}
20
21/// Map configuration options
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23pub struct MapOptions {
24    pub zoom_control: bool,
25    pub scroll_wheel_zoom: bool,
26    pub double_click_zoom: bool,
27    pub touch_zoom: bool,
28    pub dragging: bool,
29    pub keyboard: bool,
30    pub attribution_control: bool,
31    pub tile_layer: Option<TileLayer>,
32    pub leaflet_resources: LeafletResources,
33    pub crs: Option<CRS>,
34}
35
36impl Default for MapOptions {
37    fn default() -> Self {
38        Self {
39            zoom_control: true,
40            scroll_wheel_zoom: true,
41            double_click_zoom: true,
42            touch_zoom: true,
43            dragging: true,
44            keyboard: true,
45            attribution_control: true,
46            tile_layer: Some(TileLayer::default()),
47            leaflet_resources: LeafletResources::default(),
48            crs: None,
49        }
50    }
51}
52
53impl MapOptions {
54    /// Creates a MapOptions with all controls disabled and no tile layer
55    pub fn minimal() -> Self {
56        Self {
57            zoom_control: false,
58            scroll_wheel_zoom: false,
59            double_click_zoom: false,
60            touch_zoom: false,
61            dragging: false,
62            keyboard: false,
63            attribution_control: false,
64            tile_layer: None,
65            leaflet_resources: LeafletResources::default(),
66            crs: None,
67        }
68    }
69
70    /// Builder method to enable/disable zoom control
71    pub fn with_zoom_control(mut self, enabled: bool) -> Self {
72        self.zoom_control = enabled;
73        self
74    }
75
76    /// Builder method to enable/disable scroll wheel zoom
77    pub fn with_scroll_wheel_zoom(mut self, enabled: bool) -> Self {
78        self.scroll_wheel_zoom = enabled;
79        self
80    }
81
82    /// Builder method to enable/disable double click zoom
83    pub fn with_double_click_zoom(mut self, enabled: bool) -> Self {
84        self.double_click_zoom = enabled;
85        self
86    }
87
88    /// Builder method to enable/disable touch zoom
89    pub fn with_touch_zoom(mut self, enabled: bool) -> Self {
90        self.touch_zoom = enabled;
91        self
92    }
93
94    /// Builder method to enable/disable dragging
95    pub fn with_dragging(mut self, enabled: bool) -> Self {
96        self.dragging = enabled;
97        self
98    }
99
100    /// Builder method to enable/disable keyboard controls
101    pub fn with_keyboard(mut self, enabled: bool) -> Self {
102        self.keyboard = enabled;
103        self
104    }
105
106    /// Builder method to enable/disable attribution control
107    pub fn with_attribution_control(mut self, enabled: bool) -> Self {
108        self.attribution_control = enabled;
109        self
110    }
111
112    /// Builder method to set tile layer
113    pub fn with_tile_layer(mut self, tile_layer: TileLayer) -> Self {
114        self.tile_layer = Some(tile_layer);
115        self
116    }
117
118    /// Builder method to remove the tile layer (e.g. for maps made only of image overlays)
119    pub fn without_tile_layer(mut self) -> Self {
120        self.tile_layer = None;
121        self
122    }
123
124    /// Builder method to set Leaflet resources configuration
125    pub fn with_leaflet_resources(mut self, resources: LeafletResources) -> Self {
126        self.leaflet_resources = resources;
127        self
128    }
129
130    /// Builder method to set the coordinate reference system (defaults to Leaflet's EPSG3857)
131    pub fn with_crs(mut self, crs: CRS) -> Self {
132        self.crs = Some(crs);
133        self
134    }
135}