Skip to main content

tauri_plugin_geolocation/
models.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use serde::{Deserialize, Serialize};
6use tauri::plugin::PermissionState;
7
8/// The current permission state for the geolocation APIs.
9#[derive(Debug, Clone, Default, Serialize, Deserialize)]
10#[cfg_attr(feature = "specta", derive(specta::Type))]
11#[serde(rename_all = "camelCase")]
12pub struct PermissionStatus {
13    /// Permission state for the location alias.
14    ///
15    /// On Android it requests/checks both ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions.
16    ///
17    /// On iOS it requests/checks location permissions.
18    pub location: PermissionState,
19    /// Permissions state for the coarseLoaction alias.
20    ///
21    /// On Android it requests/checks ACCESS_COARSE_LOCATION.
22    ///
23    /// On Android 12+, users can choose between Approximate location (ACCESS_COARSE_LOCATION) and Precise location (ACCESS_FINE_LOCATION).
24    ///
25    /// On iOS it will have the same value as the `location` alias.
26    pub coarse_location: PermissionState,
27}
28
29/// Options used to configure a [`get_current_position`](crate::Geolocation::get_current_position) or [`watch_position`](crate::Geolocation::watch_position) request.
30#[derive(Debug, Clone, Default, Serialize, Deserialize)]
31#[cfg_attr(feature = "specta", derive(specta::Type))]
32#[serde(rename_all = "camelCase")]
33pub struct PositionOptions {
34    /// High accuracy mode (such as GPS, if available)
35    /// Will be ignored on Android 12+ if users didn't grant the ACCESS_FINE_LOCATION permission.
36    pub enable_high_accuracy: bool,
37    /// The maximum wait time in milliseconds for location updates.
38    /// Default: 10000
39    /// Ignored on iOS.
40    // TODO: Handle Infinity and default to it.
41    // TODO: Should be u64+ but specta doesn't like that?
42    pub timeout: u32,
43    /// The maximum age in milliseconds of a possible cached position that is acceptable to return.
44    /// Default: 0
45    /// Ignored on iOS.
46    // TODO: Handle Infinity.
47    // TODO: Should be u64+ but specta doesn't like that?
48    pub maximum_age: u32,
49}
50
51/// The individual permission aliases that can be requested with [`request_permissions`](crate::Geolocation::request_permissions).
52#[derive(Debug, Clone, Serialize, Deserialize)]
53#[cfg_attr(feature = "specta", derive(specta::Type))]
54#[serde(rename_all = "camelCase")]
55pub enum PermissionType {
56    /// The `location` alias. On Android this maps to both `ACCESS_COARSE_LOCATION` and `ACCESS_FINE_LOCATION`. On iOS it maps to the standard location permission.
57    Location,
58    /// The `coarseLocation` alias. On Android this maps to `ACCESS_COARSE_LOCATION` only. On iOS it behaves the same as [`Location`](Self::Location).
59    CoarseLocation,
60}
61
62/// The GPS coordinates of a [`Position`], along with the accuracy of each reading.
63#[derive(Debug, Clone, Default, Serialize, Deserialize)]
64#[cfg_attr(feature = "specta", derive(specta::Type))]
65#[serde(rename_all = "camelCase")]
66pub struct Coordinates {
67    /// Latitude in decimal degrees.
68    pub latitude: f64,
69    /// Longitude in decimal degrees.
70    pub longitude: f64,
71    /// Accuracy level of the latitude and longitude coordinates in meters.
72    pub accuracy: f64,
73    /// Accuracy level of the altitude coordinate in meters, if available.
74    /// Available on all iOS versions and on Android 8 and above.
75    pub altitude_accuracy: Option<f64>,
76    /// The altitude the user is at, if available.
77    pub altitude: Option<f64>,
78    /// The speed the user is traveling, in meters per second, if available.
79    pub speed: Option<f64>,
80    /// The heading the user is facing, if available.
81    pub heading: Option<f64>,
82}
83
84/// A geolocation reading, as returned by [`get_current_position`](crate::Geolocation::get_current_position) and reported through [`WatchEvent::Position`].
85#[derive(Debug, Clone, Default, Serialize, Deserialize)]
86#[cfg_attr(feature = "specta", derive(specta::Type))]
87#[serde(rename_all = "camelCase")]
88pub struct Position {
89    /// Creation time for these coordinates.
90    // TODO: Check if we're actually losing precision.
91    pub timestamp: u64,
92    /// The GPS coordinates along with the accuracy of the data.
93    pub coords: Coordinates,
94}
95
96/// A single update sent through the channel callback registered with [`watch_position`](crate::Geolocation::watch_position).
97#[derive(Debug, Clone, Serialize, Deserialize)]
98#[cfg_attr(feature = "specta", derive(specta::Type))]
99#[serde(untagged)]
100pub enum WatchEvent {
101    /// A new position was read successfully.
102    Position(Position),
103    /// The platform failed to read a position; the string is the platform-provided error message.
104    Error(String),
105}