leptos-use/examples/use_geolocation/src/main.rs

60 lines
1.6 KiB
Rust
Raw Normal View History

2023-09-12 23:53:43 +01:00
use leptos::*;
use leptos_use::docs::demo_or_body;
use leptos_use::{use_geolocation, UseGeolocationReturn};
#[component]
fn Demo() -> impl IntoView {
let UseGeolocationReturn {
coords,
located_at,
error,
resume,
pause,
} = use_geolocation();
view! {
<pre lang="json">
2023-09-30 18:24:06 +01:00
coords:
{move || {
if let Some(coords) = coords() {
format!(
r#"{{
2023-09-12 23:53:43 +01:00
accuracy: {},
latitude: {},
longitude: {},
altitude: {:?},
altitude_accuracy: {:?},
heading: {:?},
speed: {:?},
2023-09-30 18:24:06 +01:00
}}"#,
2023-10-23 20:15:05 -05:00
coords.accuracy(),
coords.latitude(),
coords.longitude(),
coords.altitude(),
coords.altitude_accuracy(),
coords.heading(),
coords.speed(),
2023-09-30 18:24:06 +01:00
)
} else {
"None".to_string()
}
}}
,
2023-10-23 20:15:05 -05:00
located_at: {located_at} ,
2023-09-30 18:24:06 +01:00
error:
2023-10-23 20:15:05 -05:00
{move || if let Some(error) = error() { error.message() } else { "None".to_string() }} ,
2023-09-30 18:24:06 +01:00
</pre>
<button on:click=move |_| pause()>"Pause watch"</button>
<button on:click=move |_| resume()>"Resume watch"</button>
}
2023-09-12 23:53:43 +01:00
}
fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
mount_to(demo_or_body(), || {
view! { <Demo/> }
})
}