1mod coordinates;
10pub use coordinates::{LngLat, MAX_LATITUDE, MAX_ZOOM, wrap_bearing};
11#[doc(hidden)]
12#[cfg(feature = "native-bridge")]
13#[path = "native.rs"]
14pub mod __native;
15use day_geometry::{Color, Point};
16use day_reactive::Signal;
17
18pub fn clamp_zoom(zoom: f64) -> f64 {
20 if zoom.is_finite() {
21 zoom.clamp(0.0, MAX_ZOOM)
22 } else {
23 0.0
24 }
25}
26
27#[derive(Clone, Debug, PartialEq, Eq)]
29pub enum MapStatus {
30 Loading,
32 Ready,
34 Failed(String),
36}
37
38#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
40pub struct MarkerId(u64);
41
42impl MarkerId {
43 pub const fn value(self) -> u64 {
45 self.0
46 }
47}
48
49#[derive(Clone, Debug, PartialEq)]
52#[must_use]
53pub struct Marker {
54 pub position: LngLat,
55 pub title: String,
56 pub color: Color,
57 pub subtitle: String,
59 pub tag: String,
62}
63
64impl Marker {
65 pub fn new(position: LngLat) -> Marker {
66 Marker {
67 position,
68 title: String::new(),
69 subtitle: String::new(),
70 color: Color::rgb(0.86, 0.2, 0.18),
71 tag: String::new(),
72 }
73 }
74
75 pub fn title(mut self, title: impl Into<String>) -> Marker {
76 self.title = title.into();
77 self
78 }
79
80 pub fn subtitle(mut self, text: impl Into<String>) -> Self {
81 self.subtitle = text.into();
82 self
83 }
84
85 pub fn tag(mut self, tag: impl Into<String>) -> Marker {
86 self.tag = tag.into();
87 self
88 }
89
90 pub fn color(mut self, color: Color) -> Marker {
91 self.color = color;
92 self
93 }
94}
95
96#[derive(Clone, Debug, PartialEq)]
99pub struct MapTap {
100 pub point: Point,
101 pub location: LngLat,
102 pub marker: Option<MarkerId>,
103}
104
105#[derive(Clone, Copy, Debug, PartialEq)]
107pub struct CameraRequest {
108 pub target: CameraTarget,
109 pub duration: f64,
110 pub serial: u64,
112}
113
114#[derive(Clone, Copy, Debug, PartialEq)]
115pub enum CameraTarget {
116 View {
118 center: LngLat,
119 zoom: f64,
120 bearing: Option<f64>,
121 },
122 Bounds {
124 sw: LngLat,
125 ne: LngLat,
126 padding: f64,
127 },
128}
129
130const FLY_SECS: f64 = 1.0;
132const TURN_SECS: f64 = 0.5;
134
135#[derive(Clone, Copy)]
139pub struct MapView {
140 pub center: Signal<LngLat>,
142 pub zoom: Signal<f64>,
144 pub bearing: Signal<f64>,
146 pub status: Signal<MapStatus>,
148 pub attribution: Signal<String>,
150 pub markers: Signal<Vec<(MarkerId, Marker)>>,
152 pub tap: Signal<Option<MapTap>>,
154 pub overlays: Signal<Vec<Overlay>>,
156 pub interaction: Signal<Interaction>,
158 pub request: Signal<Option<CameraRequest>>,
160 next_marker: Signal<u64>,
161}
162
163impl MapView {
164 pub fn new(center: LngLat, zoom: f64) -> MapView {
165 MapView {
166 center: Signal::new(center.wrapped()),
167 zoom: Signal::new(clamp_zoom(zoom)),
168 status: Signal::new(MapStatus::Loading),
169 attribution: Signal::new(String::new()),
170 bearing: Signal::new(0.0),
171 markers: Signal::new(Vec::new()),
172 overlays: Signal::new(Vec::new()),
173 interaction: Signal::new(Interaction::default()),
174 tap: Signal::new(None),
175 request: Signal::new(None),
176 next_marker: Signal::new(1),
177 }
178 }
179
180 pub fn add_marker(&self, marker: Marker) -> MarkerId {
182 let id = MarkerId(self.next_marker.get_untracked());
183 self.next_marker.set(id.0 + 1);
184 let mut markers = self.markers.get_untracked();
185 markers.push((id, marker));
186 self.markers.set(markers);
187 id
188 }
189
190 pub fn update_marker(&self, id: MarkerId, marker: Marker) -> bool {
193 let mut markers = self.markers.get_untracked();
194 let Some(slot) = markers.iter_mut().find(|(m, _)| *m == id) else {
195 return false;
196 };
197 slot.1 = marker;
198 self.markers.set(markers);
199 true
200 }
201
202 pub fn remove_marker(&self, id: MarkerId) {
204 let mut markers = self.markers.get_untracked();
205 markers.retain(|(m, _)| *m != id);
206 self.markers.set(markers);
207 }
208
209 pub fn clear_markers(&self) {
210 self.markers.set(Vec::new());
211 }
212
213 pub fn marker(&self, id: MarkerId) -> Option<Marker> {
215 self.markers
216 .get_untracked()
217 .into_iter()
218 .find(|(m, _)| *m == id)
219 .map(|(_, marker)| marker)
220 }
221
222 pub fn fly_to(&self, center: LngLat, zoom: f64) {
225 self.fly_to_with(center, zoom, None, FLY_SECS);
226 }
227
228 pub fn fly_to_with(&self, center: LngLat, zoom: f64, bearing: Option<f64>, duration: f64) {
232 self.request_move(
233 CameraTarget::View {
234 center: center.wrapped(),
235 zoom: clamp_zoom(zoom),
236 bearing,
237 },
238 duration,
239 );
240 }
241
242 pub fn fit_bounds(&self, sw: LngLat, ne: LngLat, padding: f64) {
245 self.request_move(
246 CameraTarget::Bounds {
247 sw: sw.wrapped(),
248 ne: ne.wrapped(),
249 padding: if padding.is_finite() {
250 padding.max(0.0)
251 } else {
252 0.0
253 },
254 },
255 FLY_SECS,
256 );
257 }
258
259 pub fn rotate_to(&self, bearing: f64) {
262 self.request_move(
263 CameraTarget::View {
264 center: self.center.get_untracked(),
265 zoom: self.zoom.get_untracked(),
266 bearing: Some(bearing),
267 },
268 TURN_SECS,
269 );
270 }
271
272 pub fn reset_north(&self) {
274 self.rotate_to(0.0);
275 }
276
277 fn request_move(&self, target: CameraTarget, duration: f64) {
278 let serial = self
279 .request
280 .get_untracked()
281 .map_or(1, |r| r.serial.wrapping_add(1));
282 self.request.set(Some(CameraRequest {
283 target,
284 duration: if duration.is_finite() {
285 duration.clamp(0.0, 60.0)
286 } else {
287 0.0
288 },
289 serial,
290 }));
291 }
292
293 pub fn zoom_by(&self, levels: f64) {
295 let z = clamp_zoom(self.zoom.get_untracked() + levels);
296 self.zoom.set(z);
297 }
298
299 pub fn jump_to(&self, center: LngLat, zoom: f64) {
301 self.center.set(center.wrapped());
302 self.zoom.set(clamp_zoom(zoom));
303 }
304
305 pub fn jump_to_with(&self, center: LngLat, zoom: f64, bearing: f64) {
307 self.jump_to(center, zoom);
308 self.bearing.set(wrap_bearing(bearing));
309 }
310}
311
312#[derive(Clone, Copy, Debug, PartialEq, Eq)]
314pub struct Interaction {
315 pub pan: bool,
316 pub zoom: bool,
317 pub rotate: bool,
318}
319impl Default for Interaction {
320 fn default() -> Self {
321 Self {
322 pan: true,
323 zoom: true,
324 rotate: true,
325 }
326 }
327}
328
329#[derive(Clone, Debug, PartialEq)]
332#[non_exhaustive]
333pub enum Overlay {
334 Polyline {
335 points: Vec<LngLat>,
336 color: Color,
337 width: f64,
338 },
339 Polygon {
340 points: Vec<LngLat>,
341 fill: Color,
342 stroke: Color,
343 width: f64,
344 },
345 Circle {
346 center: LngLat,
347 radius: f64,
348 fill: Color,
349 stroke: Color,
350 width: f64,
351 },
352}