Skip to main content

dioxus_maplibre/handle/
sources.rs

1//! Source-related MapHandle methods.
2#![allow(clippy::needless_pass_by_value)]
3
4use super::MapHandle;
5use crate::options::{
6    GeoJsonSourceOptions, ImageSourceOptions, RasterDemSourceOptions, RasterSourceOptions,
7    VectorSourceOptions,
8};
9
10impl MapHandle {
11    /// Add a GeoJSON source to the map
12    pub fn add_geojson_source(&self, id: &str, options: GeoJsonSourceOptions) {
13        self.fire_and_forget(|| {
14            let json = serde_json::to_string(&options).unwrap_or_default();
15            crate::interop::add_geojson_source_js(&self.map_id, id, &json)
16        });
17    }
18
19    /// Add a vector tile source to the map
20    pub fn add_vector_source(&self, id: &str, options: VectorSourceOptions) {
21        self.fire_and_forget(|| {
22            let json = serde_json::to_string(&options).unwrap_or_default();
23            crate::interop::add_vector_source_js(&self.map_id, id, &json)
24        });
25    }
26
27    /// Add a raster tile source to the map
28    pub fn add_raster_source(&self, id: &str, options: RasterSourceOptions) {
29        self.fire_and_forget(|| {
30            let json = serde_json::to_string(&options).unwrap_or_default();
31            crate::interop::add_raster_source_js(&self.map_id, id, &json)
32        });
33    }
34
35    /// Add a raster DEM source (for terrain)
36    pub fn add_raster_dem_source(&self, id: &str, options: RasterDemSourceOptions) {
37        self.fire_and_forget(|| {
38            let json = serde_json::to_string(&options).unwrap_or_default();
39            crate::interop::add_raster_dem_source_js(&self.map_id, id, &json)
40        });
41    }
42
43    /// Add an image source to the map
44    pub fn add_image_source(&self, id: &str, options: ImageSourceOptions) {
45        self.fire_and_forget(|| {
46            let json = serde_json::to_string(&options).unwrap_or_default();
47            crate::interop::add_image_source_js(&self.map_id, id, &json)
48        });
49    }
50
51    /// Update the data of an existing GeoJSON source
52    pub fn update_geojson_source(&self, id: &str, data: serde_json::Value) {
53        self.fire_and_forget(|| {
54            let json = serde_json::to_string(&data).unwrap_or_default();
55            crate::interop::update_geojson_source_js(&self.map_id, id, &json)
56        });
57    }
58
59    /// Remove a source from the map
60    pub fn remove_source(&self, id: &str) {
61        self.fire_and_forget(|| crate::interop::remove_source_js(&self.map_id, id));
62    }
63}