Skip to main content

leptos_use_geolocation/
options.rs

1use default_struct_builder::DefaultBuilder;
2
3/// Options for [`use_geolocation_with_options`](crate::use_geolocation_with_options).
4#[derive(DefaultBuilder, Clone)]
5#[allow(dead_code)]
6pub struct UseGeolocationOptions {
7    /// If `true` the geolocation watch is started when this function is called.
8    /// If `false` you have to call `resume` manually to start it. Defaults to `true`.
9    pub(crate) immediate: bool,
10
11    /// A boolean value that indicates the application would like to receive the best
12    /// possible results. If `true` and if the device is able to provide a more accurate
13    /// position, it will do so. Note that this can result in slower response times or
14    /// increased power consumption (with a GPS chip on a mobile device for example).
15    /// On the other hand, if `false`, the device can take the liberty to save
16    /// resources by responding more quickly and/or using less power. Default: `false`.
17    enable_high_accuracy: bool,
18
19    /// A positive value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return.
20    /// If set to `0`, it means that the device cannot use a cached position and must attempt to retrieve the real current position.
21    /// Default: 30000.
22    maximum_age: u32,
23
24    /// A positive value representing the maximum length of time (in milliseconds)
25    /// the device is allowed to take in order to return a position.
26    /// The default value is 27000.
27    timeout: u32,
28}
29
30impl Default for UseGeolocationOptions {
31    fn default() -> Self {
32        Self {
33            enable_high_accuracy: false,
34            maximum_age: 30000,
35            timeout: 27000,
36            immediate: true,
37        }
38    }
39}
40
41#[cfg(not(feature = "ssr"))]
42impl UseGeolocationOptions {
43    pub(crate) fn as_position_options(&self) -> web_sys::PositionOptions {
44        let UseGeolocationOptions {
45            enable_high_accuracy,
46            maximum_age,
47            timeout,
48            ..
49        } = self;
50
51        let options = web_sys::PositionOptions::new();
52        options.set_enable_high_accuracy(*enable_high_accuracy);
53        options.set_maximum_age(*maximum_age);
54        options.set_timeout(*timeout);
55
56        options
57    }
58}