Skip to main content

dioxus_leaflet/types/
latlng.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
4pub struct LatLng {
5    pub lat: f64,
6    pub lng: f64,
7    pub alt: Option<f64>,
8}
9
10impl LatLng {
11    pub const fn new(lat: f64, lng: f64) -> Self {
12        Self {
13            lat,
14            lng,
15            alt: None,
16        }
17    }
18}
19
20impl std::ops::Add for LatLng {
21    type Output = LatLng;
22    fn add(self, rhs: Self) -> Self::Output {
23        Self {
24            lat: self.lat + rhs.lat,
25            lng: self.lng + rhs.lng,
26            alt: match (self.alt, rhs.alt) {
27                (Some(a), Some(b)) => Some(a + b),
28                (Some(a), None) => Some(a),
29                (None, Some(b)) => Some(b),
30                (None, None) => None,
31            },
32        }
33    }
34}
35
36impl std::ops::AddAssign for LatLng {
37    fn add_assign(&mut self, rhs: Self) {
38        self.lat += rhs.lat;
39        self.lng += rhs.lng;
40        self.alt = match (self.alt, rhs.alt) {
41            (Some(a), Some(b)) => Some(a + b),
42            (Some(a), None) => Some(a),
43            (None, Some(b)) => Some(b),
44            (None, None) => None,
45        };
46    }
47}