Skip to main content

__location__

Function __location__ 

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

Capture the user’s geographic location by opening the browser to a local consent page.

This is a blocking call. It:

  1. Binds a temporary HTTP server on a free 127.0.0.1 port.
  2. Opens http://127.0.0.1:<port>/ in the system’s default browser.
  3. Waits for the browser to POST location data (or an error) back.
  4. Shuts the server down and returns the result.

Pass a PageTemplate to control what the user sees in the browser. Use PageTemplate::default() for the built-in single-button consent page.

§Errors

VariantCause
LocationError::PermissionDeniedUser denied the browser prompt.
LocationError::PositionUnavailableDevice cannot determine position.
LocationError::TimeoutNo fix within the browser’s 30 s timeout.
LocationError::ServerErrorFailed to start the local HTTP server or runtime.

§Example

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

// Default built-in page
match __location__(PageTemplate::default()) {
    Ok(data) => println!("lat={:.6} lon={:.6} ±{:.0}m",
                         data.latitude, data.longitude, data.accuracy),
    Err(LocationError::PermissionDenied) => eprintln!("access denied"),
    Err(e) => eprintln!("error: {e}"),
}

// Tickbox page with custom consent label
let _ = __location__(PageTemplate::Tickbox {
    title:        None,
    body_text:    None,
    consent_text: Some("I agree to share my location.".into()),
});

§Note on async callers

If you are already inside a Tokio async context (e.g. inside #[tokio::main]), use __location_async__ instead — it is the raw async fn and avoids spawning an extra OS thread.