1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[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 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#[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 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 pub fn with_description(mut self, description: impl Into<String>) -> Self {
53 self.description = Some(description.into());
54 self
55 }
56
57 pub fn with_icon(mut self, icon: MarkerIcon) -> Self {
59 self.icon = Some(icon);
60 self
61 }
62
63 pub fn with_popup_options(mut self, options: PopupOptions) -> Self {
65 self.popup_options = Some(options);
66 self
67 }
68
69 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#[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 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#[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
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
178impl MapOptions {
179 pub fn minimal() -> Self {
181 Self {
182 zoom_control: false,
183 scroll_wheel_zoom: false,
184 double_click_zoom: false,
185 touch_zoom: false,
186 dragging: false,
187 keyboard: false,
188 attribution_control: false,
189 tile_layer: TileLayer::default(),
190 }
191 }
192
193 pub fn with_zoom_control(mut self, enabled: bool) -> Self {
195 self.zoom_control = enabled;
196 self
197 }
198
199 pub fn with_scroll_wheel_zoom(mut self, enabled: bool) -> Self {
201 self.scroll_wheel_zoom = enabled;
202 self
203 }
204
205 pub fn with_double_click_zoom(mut self, enabled: bool) -> Self {
207 self.double_click_zoom = enabled;
208 self
209 }
210
211 pub fn with_touch_zoom(mut self, enabled: bool) -> Self {
213 self.touch_zoom = enabled;
214 self
215 }
216
217 pub fn with_dragging(mut self, enabled: bool) -> Self {
219 self.dragging = enabled;
220 self
221 }
222
223 pub fn with_keyboard(mut self, enabled: bool) -> Self {
225 self.keyboard = enabled;
226 self
227 }
228
229 pub fn with_attribution_control(mut self, enabled: bool) -> Self {
231 self.attribution_control = enabled;
232 self
233 }
234
235 pub fn with_tile_layer(mut self, tile_layer: TileLayer) -> Self {
237 self.tile_layer = tile_layer;
238 self
239 }
240}
241
242#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
244pub struct TileLayer {
245 pub url: String,
246 pub attribution: String,
247 pub max_zoom: u8,
248 pub subdomains: Vec<String>,
249}
250
251impl TileLayer {
252 pub fn openstreetmap() -> Self {
254 Self {
255 url: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png".to_string(),
256 attribution: "© <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors".to_string(),
257 max_zoom: 19,
258 subdomains: vec!["a".to_string(), "b".to_string(), "c".to_string()],
259 }
260 }
261
262 pub fn satellite() -> Self {
264 Self {
265 url: "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}".to_string(),
266 attribution: "Tiles © Esri".to_string(),
267 max_zoom: 18,
268 subdomains: vec![],
269 }
270 }
271}
272
273impl Default for TileLayer {
274 fn default() -> Self {
275 Self::openstreetmap()
276 }
277}