Skip to main content

dioxus_maplibre/handle/
navigation.rs

1//! Navigation and camera MapHandle methods.
2#![allow(clippy::needless_pass_by_value)]
3
4use super::MapHandle;
5use crate::options::{EaseToOptions, FitBoundsOptions, FlyToOptions, JumpToOptions};
6use crate::types::{Bounds, LatLng};
7
8impl MapHandle {
9    /// Fly to a location with animation
10    pub fn fly_to(&self, options: FlyToOptions) {
11        self.fire_and_forget(|| {
12            let json = serde_json::to_string(&options).unwrap_or_default();
13            crate::interop::fly_to_js(&self.map_id, &json)
14        });
15    }
16
17    /// Ease to a location with animation
18    pub fn ease_to(&self, options: EaseToOptions) {
19        self.fire_and_forget(|| {
20            let json = serde_json::to_string(&options).unwrap_or_default();
21            crate::interop::ease_to_js(&self.map_id, &json)
22        });
23    }
24
25    /// Jump to a location instantly (no animation)
26    pub fn jump_to(&self, options: JumpToOptions) {
27        self.fire_and_forget(|| {
28            let json = serde_json::to_string(&options).unwrap_or_default();
29            crate::interop::jump_to_js(&self.map_id, &json)
30        });
31    }
32
33    /// Fit the map to the given bounds
34    pub fn fit_bounds(&self, bounds: Bounds, options: FitBoundsOptions) {
35        self.fire_and_forget(|| {
36            let json = serde_json::to_string(&options).unwrap_or_default();
37            crate::interop::fit_bounds_js(
38                &self.map_id,
39                bounds.sw.lng,
40                bounds.sw.lat,
41                bounds.ne.lng,
42                bounds.ne.lat,
43                &json,
44            )
45        });
46    }
47
48    /// Pan to a coordinate
49    pub fn pan_to(&self, position: LatLng) {
50        self.fire_and_forget(|| {
51            crate::interop::pan_to_js(&self.map_id, position.lat, position.lng)
52        });
53    }
54
55    /// Pan by pixel offset (instant, no animation)
56    pub fn pan_by(&self, x: i32, y: i32) {
57        self.fire_and_forget(|| crate::interop::pan_by_js(&self.map_id, x, y));
58    }
59
60    /// Set zoom level
61    pub fn zoom_to(&self, zoom: f64) {
62        self.fire_and_forget(|| crate::interop::zoom_to_js(&self.map_id, zoom));
63    }
64
65    /// Zoom in one level
66    pub fn zoom_in(&self) {
67        self.fire_and_forget(|| crate::interop::zoom_in_js(&self.map_id));
68    }
69
70    /// Zoom out one level
71    pub fn zoom_out(&self) {
72        self.fire_and_forget(|| crate::interop::zoom_out_js(&self.map_id));
73    }
74
75    /// Set bearing (rotation)
76    pub fn rotate_to(&self, bearing: f64) {
77        self.fire_and_forget(|| crate::interop::rotate_to_js(&self.map_id, bearing));
78    }
79
80    /// Set pitch (tilt)
81    pub fn set_pitch(&self, pitch: f64) {
82        self.fire_and_forget(|| crate::interop::set_pitch_js(&self.map_id, pitch));
83    }
84
85    /// Reset bearing to north (0 degrees)
86    pub fn reset_north(&self) {
87        self.fire_and_forget(|| crate::interop::reset_north_js(&self.map_id));
88    }
89
90    /// Set throttle for `on_move` events in milliseconds (0 = every animation frame)
91    pub fn set_move_event_throttle(&self, throttle_ms: u32) {
92        self.fire_and_forget(|| {
93            crate::interop::set_move_event_throttle_js(&self.map_id, throttle_ms)
94        });
95    }
96}