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    /// On Android the timeout gets ignored for getCurrentPosition.
40    /// Ignored on iOS.
41    // TODO: Handle Infinity and default to it.
42    // TODO: Should be u64+ but specta doesn't like that?
43    pub timeout: u32,
44    /// The maximum age in milliseconds of a possible cached position that is acceptable to return.
45    /// Default: 0
46    /// Ignored on iOS.
47    // TODO: Handle Infinity.
48    // TODO: Should be u64+ but specta doesn't like that?
49    pub maximum_age: u32,
50}
51
52/// The individual permission aliases that can be requested with [`request_permissions`](crate::Geolocation::request_permissions).
53#[derive(Debug, Clone, Serialize, Deserialize)]
54#[cfg_attr(feature = "specta", derive(specta::Type))]
55#[serde(rename_all = "camelCase")]
56pub enum PermissionType {
57    /// 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.
58    Location,
59    /// The `coarseLocation` alias. On Android this maps to `ACCESS_COARSE_LOCATION` only. On iOS it behaves the same as [`Location`](Self::Location).
60    CoarseLocation,
61}
62
63/// The GPS coordinates of a [`Position`], along with the accuracy of each reading.
64#[derive(Debug, Clone, Default, Serialize, Deserialize)]
65#[cfg_attr(feature = "specta", derive(specta::Type))]
66#[serde(rename_all = "camelCase")]
67pub struct Coordinates {
68    /// Latitude in decimal degrees.
69    pub latitude: f64,
70    /// Longitude in decimal degrees.
71    pub longitude: f64,
72    /// Accuracy level of the latitude and longitude coordinates in meters.
73    pub accuracy: f64,
74    /// Accuracy level of the altitude coordinate in meters, if available.
75    /// Available on all iOS versions and on Android 8 and above.
76    pub altitude_accuracy: Option<f64>,
77    /// The altitude the user is at, if available.
78    pub altitude: Option<f64>,
79    /// The speed the user is traveling, in meters per second, if available.
80    pub speed: Option<f64>,
81    /// The heading the user is facing, if available.
82    pub heading: Option<f64>,
83}
84
85/// A geolocation reading, as returned by [`get_current_position`](crate::Geolocation::get_current_position) and reported through [`WatchEvent::Position`].
86#[derive(Debug, Clone, Default, Serialize, Deserialize)]
87#[cfg_attr(feature = "specta", derive(specta::Type))]
88#[serde(rename_all = "camelCase")]
89pub struct Position {
90    /// Creation time for these coordinates.
91    // TODO: Check if we're actually losing precision.
92    pub timestamp: u64,
93    /// The GPS coordinates along with the accuracy of the data.
94    pub coords: Coordinates,
95}
96
97/// A single update sent through the channel callback registered with [`watch_position`](crate::Geolocation::watch_position).
98#[derive(Debug, Clone, Serialize, Deserialize)]
99#[cfg_attr(feature = "specta", derive(specta::Type))]
100#[serde(untagged)]
101pub enum WatchEvent {
102    /// A new position was read successfully.
103    Position(Position),
104    /// The platform failed to read a position; the string is the platform-provided error message.
105    Error(String),
106}