Skip to main content

__location_async__

Function __location_async__ 

Source
pub async fn __location_async__(
    template: PageTemplate,
) -> Result<LocationData, LocationError>
Expand description

Async version of __location__.

Directly awaits the capture future — preferred when you are already running inside a Tokio async context so no extra OS thread needs to be spawned.

§Example

use toolkit_zero::location::browser::{__location_async__, PageTemplate, LocationError};

#[tokio::main]
async fn main() {
    match __location_async__(PageTemplate::default()).await {
        Ok(data) => println!("lat={:.6} lon={:.6}", data.latitude, data.longitude),
        Err(e) => eprintln!("error: {e}"),
    }
}
Examples found in repository?
examples/location_browser.rs (line 121)
116async fn main() {
117    println!("=== location_browser example ===\n");
118
119    // ── 1. Async call (preferred inside #[tokio::main]) ───────────────────────
120    println!("Opening browser to acquire location (async)…");
121    match __location_async__(PageTemplate::default()).await {
122        Ok(data) => print_location("Async result:", &data),
123        Err(LocationError::PermissionDenied) => {
124            eprintln!("Location permission denied — skipping async call.");
125        }
126        Err(e) => eprintln!("Async location error: {e}"),
127    }
128
129    println!();
130
131    // ── 2. Blocking call (also safe inside #[tokio::main]) ────────────────────
132    // __location__ detects the existing Tokio runtime and offloads to a
133    // dedicated OS thread automatically — no runtime nesting issue.
134    println!("Opening browser to acquire location (blocking)…");
135    let template = PageTemplate::Tickbox {
136        title:        Some("Confirm Location".into()),
137        body_text:    None,
138        consent_text: Some("I allow this example to read my location.".into()),
139    };
140    match __location__(template) {
141        Ok(data) => print_location("Blocking result:", &data),
142        Err(LocationError::PermissionDenied) => {
143            eprintln!("Location permission denied — skipping blocking call.");
144        }
145        Err(e) => eprintln!("Blocking location error: {e}"),
146    }
147}