dioxus_leaflet/
types.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4/// Represents a geographical position with zoom level
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub struct MapPosition {
7    pub lat: f64,
8    pub lng: f64,
9    pub zoom: f64,
10}
11
12impl MapPosition {
13    /// Creates a new MapPosition
14    pub fn new(lat: f64, lng: f64, zoom: f64) -> Self {
15        Self { lat, lng, zoom }
16    }
17}
18
19impl Default for MapPosition {
20    fn default() -> Self {
21        Self::new(51.505, -0.09, 13.0)
22    }
23}
24
25/// Represents a marker on the map
26#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
27pub struct MapMarker {
28    pub lat: f64,
29    pub lng: f64,
30    pub title: String,
31    pub description: Option<String>,
32    pub icon: Option<MarkerIcon>,
33    pub popup_options: Option<PopupOptions>,
34    pub custom_data: Option<HashMap<String, String>>,
35}
36
37impl MapMarker {
38    /// Creates a new MapMarker with basic information
39    pub fn new(lat: f64, lng: f64, title: impl Into<String>) -> Self {
40        Self {
41            lat,
42            lng,
43            title: title.into(),
44            description: None,
45            icon: None,
46            popup_options: None,
47            custom_data: None,
48        }
49    }
50
51    /// Adds a description to the marker
52    pub fn with_description(mut self, description: impl Into<String>) -> Self {
53        self.description = Some(description.into());
54        self
55    }
56
57    /// Adds a custom icon to the marker
58    pub fn with_icon(mut self, icon: MarkerIcon) -> Self {
59        self.icon = Some(icon);
60        self
61    }
62
63    /// Adds custom popup options
64    pub fn with_popup_options(mut self, options: PopupOptions) -> Self {
65        self.popup_options = Some(options);
66        self
67    }
68
69    /// Adds custom data
70    pub fn with_custom_data(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
71        if self.custom_data.is_none() {
72            self.custom_data = Some(HashMap::new());
73        }
74        if let Some(ref mut data) = self.custom_data {
75            data.insert(key.into(), value.into());
76        }
77        self
78    }
79}
80
81impl Default for MapMarker {
82    fn default() -> Self {
83        Self {
84            lat: 0.0,
85            lng: 0.0,
86            title: String::new(),
87            description: None,
88            icon: None,
89            popup_options: None,
90            custom_data: None,
91        }
92    }
93}
94
95/// Custom marker icon configuration
96#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
97pub struct MarkerIcon {
98    pub icon_url: String,
99    pub icon_size: Option<(u32, u32)>,
100    pub icon_anchor: Option<(u32, u32)>,
101    pub popup_anchor: Option<(i32, i32)>,
102    pub shadow_url: Option<String>,
103    pub shadow_size: Option<(u32, u32)>,
104}
105
106impl MarkerIcon {
107    /// Creates a new MarkerIcon with just the URL
108    pub fn new(icon_url: impl Into<String>) -> Self {
109        Self {
110            icon_url: icon_url.into(),
111            icon_size: None,
112            icon_anchor: None,
113            popup_anchor: None,
114            shadow_url: None,
115            shadow_size: None,
116        }
117    }
118}
119
120/// Popup configuration options
121#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
122pub struct PopupOptions {
123    pub max_width: Option<u32>,
124    pub min_width: Option<u32>,
125    pub max_height: Option<u32>,
126    pub auto_pan: Option<bool>,
127    pub keep_in_view: Option<bool>,
128    pub close_button: Option<bool>,
129    pub auto_close: Option<bool>,
130    pub close_on_escape_key: Option<bool>,
131    pub class_name: Option<String>,
132}
133
134impl Default for PopupOptions {
135    fn default() -> Self {
136        Self {
137            max_width: Some(300),
138            min_width: Some(50),
139            max_height: None,
140            auto_pan: Some(true),
141            keep_in_view: Some(false),
142            close_button: Some(true),
143            auto_close: Some(true),
144            close_on_escape_key: Some(true),
145            class_name: None,
146        }
147    }
148}
149
150/// Map configuration options
151#[derive(Debug, Clone, PartialEq)]
152pub struct MapOptions {
153    pub zoom_control: bool,
154    pub scroll_wheel_zoom: bool,
155    pub double_click_zoom: bool,
156    pub touch_zoom: bool,
157    pub dragging: bool,
158    pub keyboard: bool,
159    pub attribution_control: bool,
160    pub tile_layer: TileLayer,
161}
162
163impl Default for MapOptions {
164    fn default() -> Self {
165        Self {
166            zoom_control: true,
167            scroll_wheel_zoom: true,
168            double_click_zoom: true,
169            touch_zoom: true,
170            dragging: true,
171            keyboard: true,
172            attribution_control: true,
173            tile_layer: TileLayer::default(),
174        }
175    }
176}
177
178/// Tile layer configuration
179#[derive(Debug, Clone, PartialEq)]
180pub struct TileLayer {
181    pub url: String,
182    pub attribution: String,
183    pub max_zoom: u8,
184    pub subdomains: Vec<String>,
185}
186
187impl TileLayer {
188    /// OpenStreetMap tile layer (default)
189    pub fn openstreetmap() -> Self {
190        Self {
191            url: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png".to_string(),
192            attribution: "&copy; <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors".to_string(),
193            max_zoom: 19,
194            subdomains: vec!["a".to_string(), "b".to_string(), "c".to_string()],
195        }
196    }
197
198    /// Satellite imagery tile layer
199    pub fn satellite() -> Self {
200        Self {
201            url: "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}".to_string(),
202            attribution: "Tiles &copy; Esri".to_string(),
203            max_zoom: 18,
204            subdomains: vec![],
205        }
206    }
207}
208
209impl Default for TileLayer {
210    fn default() -> Self {
211        Self::openstreetmap()
212    }
213}