@@ -2,13 +2,20 @@ use dioxus::prelude::*;
22use dioxus_std:: geolocation:: { init_geolocator, use_geolocation, PowerMode } ;
33
44fn main ( ) {
5- dioxus_desktop:: launch ( app) ;
6- //dioxus_web::launch(app);
5+ launch ( app) ;
76}
87
98fn app ( ) -> Element {
10- let geolocator = init_geolocator ( PowerMode :: High ) . unwrap ( ) ;
11- let initial_coords = use_future ( |_| async move { geolocator. get_coordinates ( ) . await . unwrap ( ) } ) ;
9+ let geolocator = init_geolocator ( PowerMode :: High ) ;
10+ let initial_coords = use_resource ( move || async move {
11+ geolocator
12+ . read ( )
13+ . as_ref ( )
14+ . unwrap ( )
15+ . get_coordinates ( )
16+ . await
17+ . unwrap ( )
18+ } ) ;
1219 let latest_coords = use_geolocation ( ) ;
1320
1421 let latest_coords = match latest_coords {
@@ -22,24 +29,22 @@ fn app() -> Element {
2229 // Google maps embed api key
2330 //let key = std::env::var("DIOXUS_GEOLOCATION_MAP_KEY").unwrap();
2431
25- let initial_coords = initial_coords. value ( ) ;
26-
2732 rsx ! (
2833 div {
2934 style: "text-align: center;" ,
3035 h1 { "🗺️ Dioxus Geolocation Example 🛰️" }
3136 h3 { "Your initial location is:" }
3237
3338 p {
34- if let Some ( coords) = initial_coords {
35- format! ( "Latitude: {} | Longitude: {}" , coords. latitude , coords . longitude)
39+ if let Some ( coords) = initial_coords. read ( ) . as_ref ( ) {
40+ "Latitude: {coords.latitude } | Longitude: {coords.longitude}"
3641 } else {
37- "Loading..." . to_string ( )
42+ "Loading..."
3843 }
3944 }
4045
4146 h3 { "Your latest location is:" }
42- p { format! ( "Latitude: {} | Longitude: {}" , latest_coords. latitude , latest_coords . longitude) }
47+ p { "Latitude: {latest_coords.latitude } | Longitude: {latest_coords.longitude}" }
4348
4449 // Google maps embed
4550 //iframe {
0 commit comments