Skip to main content

dioxus_maplibre/handle/
images.rs

1//! Image sprite MapHandle methods.
2#![allow(clippy::unused_async)]
3
4use super::MapHandle;
5#[cfg(target_arch = "wasm32")]
6use dioxus::prelude::document;
7
8impl MapHandle {
9    /// Load an image from a URL and add it to the map's sprite
10    pub fn load_image(&self, id: &str, url: &str) {
11        self.fire_and_forget(|| crate::interop::load_image_js(&self.map_id, id, url));
12    }
13
14    /// Load an image and wait for it to complete (returns true on success)
15    #[cfg(target_arch = "wasm32")]
16    pub async fn load_image_async(&self, id: &str, url: &str) -> bool {
17        let js = crate::interop::load_image_async_js(&self.map_id, id, url);
18        document::eval(&js).join::<bool>().await.unwrap_or(false)
19    }
20
21    /// Load an image and wait for it to complete (returns true on success)
22    #[cfg(not(target_arch = "wasm32"))]
23    pub async fn load_image_async(&self, _id: &str, _url: &str) -> bool {
24        false
25    }
26
27    /// Check if an image exists in the map's sprite
28    #[cfg(target_arch = "wasm32")]
29    pub async fn has_image(&self, id: &str) -> bool {
30        let js = crate::interop::has_image_js(&self.map_id, id);
31        document::eval(&js).join::<bool>().await.unwrap_or(false)
32    }
33
34    /// Check if an image exists in the map's sprite
35    #[cfg(not(target_arch = "wasm32"))]
36    pub async fn has_image(&self, _id: &str) -> bool {
37        false
38    }
39
40    /// Remove an image from the map's sprite
41    pub fn remove_image(&self, id: &str) {
42        self.fire_and_forget(|| crate::interop::remove_image_js(&self.map_id, id));
43    }
44}