mod coordinates;
pub use coordinates::{LngLat, MAX_LATITUDE, MAX_ZOOM, wrap_bearing};
#[doc(hidden)]
#[cfg(feature = "native-bridge")]
#[path = "native.rs"]
pub mod __native;
use day_geometry::{Color, Point};
use day_reactive::Signal;
pub fn clamp_zoom(zoom: f64) -> f64 {
if zoom.is_finite() {
zoom.clamp(0.0, MAX_ZOOM)
} else {
0.0
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MapStatus {
Loading,
Ready,
Failed(String),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MarkerId(u64);
impl MarkerId {
pub const fn value(self) -> u64 {
self.0
}
}
#[derive(Clone, Debug, PartialEq)]
#[must_use]
pub struct Marker {
pub position: LngLat,
pub title: String,
pub color: Color,
pub subtitle: String,
pub tag: String,
}
impl Marker {
pub fn new(position: LngLat) -> Marker {
Marker {
position,
title: String::new(),
subtitle: String::new(),
color: Color::rgb(0.86, 0.2, 0.18),
tag: String::new(),
}
}
pub fn title(mut self, title: impl Into<String>) -> Marker {
self.title = title.into();
self
}
pub fn subtitle(mut self, text: impl Into<String>) -> Self {
self.subtitle = text.into();
self
}
pub fn tag(mut self, tag: impl Into<String>) -> Marker {
self.tag = tag.into();
self
}
pub fn color(mut self, color: Color) -> Marker {
self.color = color;
self
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct MapTap {
pub point: Point,
pub location: LngLat,
pub marker: Option<MarkerId>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CameraRequest {
pub target: CameraTarget,
pub duration: f64,
pub serial: u64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CameraTarget {
View {
center: LngLat,
zoom: f64,
bearing: Option<f64>,
},
Bounds {
sw: LngLat,
ne: LngLat,
padding: f64,
},
}
const FLY_SECS: f64 = 1.0;
const TURN_SECS: f64 = 0.5;
#[derive(Clone, Copy)]
pub struct MapView {
pub center: Signal<LngLat>,
pub zoom: Signal<f64>,
pub bearing: Signal<f64>,
pub status: Signal<MapStatus>,
pub attribution: Signal<String>,
pub markers: Signal<Vec<(MarkerId, Marker)>>,
pub tap: Signal<Option<MapTap>>,
pub overlays: Signal<Vec<Overlay>>,
pub interaction: Signal<Interaction>,
pub request: Signal<Option<CameraRequest>>,
next_marker: Signal<u64>,
}
impl MapView {
pub fn new(center: LngLat, zoom: f64) -> MapView {
MapView {
center: Signal::new(center.wrapped()),
zoom: Signal::new(clamp_zoom(zoom)),
status: Signal::new(MapStatus::Loading),
attribution: Signal::new(String::new()),
bearing: Signal::new(0.0),
markers: Signal::new(Vec::new()),
overlays: Signal::new(Vec::new()),
interaction: Signal::new(Interaction::default()),
tap: Signal::new(None),
request: Signal::new(None),
next_marker: Signal::new(1),
}
}
pub fn add_marker(&self, marker: Marker) -> MarkerId {
let id = MarkerId(self.next_marker.get_untracked());
self.next_marker.set(id.0 + 1);
let mut markers = self.markers.get_untracked();
markers.push((id, marker));
self.markers.set(markers);
id
}
pub fn update_marker(&self, id: MarkerId, marker: Marker) -> bool {
let mut markers = self.markers.get_untracked();
let Some(slot) = markers.iter_mut().find(|(m, _)| *m == id) else {
return false;
};
slot.1 = marker;
self.markers.set(markers);
true
}
pub fn remove_marker(&self, id: MarkerId) {
let mut markers = self.markers.get_untracked();
markers.retain(|(m, _)| *m != id);
self.markers.set(markers);
}
pub fn clear_markers(&self) {
self.markers.set(Vec::new());
}
pub fn marker(&self, id: MarkerId) -> Option<Marker> {
self.markers
.get_untracked()
.into_iter()
.find(|(m, _)| *m == id)
.map(|(_, marker)| marker)
}
pub fn fly_to(&self, center: LngLat, zoom: f64) {
self.fly_to_with(center, zoom, None, FLY_SECS);
}
pub fn fly_to_with(&self, center: LngLat, zoom: f64, bearing: Option<f64>, duration: f64) {
self.request_move(
CameraTarget::View {
center: center.wrapped(),
zoom: clamp_zoom(zoom),
bearing,
},
duration,
);
}
pub fn fit_bounds(&self, sw: LngLat, ne: LngLat, padding: f64) {
self.request_move(
CameraTarget::Bounds {
sw: sw.wrapped(),
ne: ne.wrapped(),
padding: if padding.is_finite() {
padding.max(0.0)
} else {
0.0
},
},
FLY_SECS,
);
}
pub fn rotate_to(&self, bearing: f64) {
self.request_move(
CameraTarget::View {
center: self.center.get_untracked(),
zoom: self.zoom.get_untracked(),
bearing: Some(bearing),
},
TURN_SECS,
);
}
pub fn reset_north(&self) {
self.rotate_to(0.0);
}
fn request_move(&self, target: CameraTarget, duration: f64) {
let serial = self
.request
.get_untracked()
.map_or(1, |r| r.serial.wrapping_add(1));
self.request.set(Some(CameraRequest {
target,
duration: if duration.is_finite() {
duration.clamp(0.0, 60.0)
} else {
0.0
},
serial,
}));
}
pub fn zoom_by(&self, levels: f64) {
let z = clamp_zoom(self.zoom.get_untracked() + levels);
self.zoom.set(z);
}
pub fn jump_to(&self, center: LngLat, zoom: f64) {
self.center.set(center.wrapped());
self.zoom.set(clamp_zoom(zoom));
}
pub fn jump_to_with(&self, center: LngLat, zoom: f64, bearing: f64) {
self.jump_to(center, zoom);
self.bearing.set(wrap_bearing(bearing));
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Interaction {
pub pan: bool,
pub zoom: bool,
pub rotate: bool,
}
impl Default for Interaction {
fn default() -> Self {
Self {
pan: true,
zoom: true,
rotate: true,
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Overlay {
Polyline {
points: Vec<LngLat>,
color: Color,
width: f64,
},
Polygon {
points: Vec<LngLat>,
fill: Color,
stroke: Color,
width: f64,
},
Circle {
center: LngLat,
radius: f64,
fill: Color,
stroke: Color,
width: f64,
},
}