|
| 1 | +/// Geolocation API types matching the MDN Web API. |
| 2 | +/// |
| 3 | +/// See: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API |
| 4 | +library; |
| 5 | + |
| 6 | +// --------------------------------------------------------------------------- |
| 7 | +// Callbacks |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | + |
| 10 | +typedef PositionCallback = void Function(GeolocationPosition position); |
| 11 | +typedef PositionErrorCallback = void Function(GeolocationPositionError error); |
| 12 | + |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | +// GeolocationPosition |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | + |
| 17 | +/// Represents the position of the concerned device at a given time. |
| 18 | +/// |
| 19 | +/// See: https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPosition |
| 20 | +abstract class GeolocationPosition { |
| 21 | + /// The coordinates defining the current location. |
| 22 | + GeolocationCoordinates get coords; |
| 23 | + |
| 24 | + /// The time at which the location was retrieved (milliseconds since epoch). |
| 25 | + int get timestamp; |
| 26 | +} |
| 27 | + |
| 28 | +// --------------------------------------------------------------------------- |
| 29 | +// GeolocationCoordinates |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | + |
| 32 | +/// Represents the position and altitude of the device on Earth, as well as the |
| 33 | +/// accuracy with which these properties are calculated. |
| 34 | +/// |
| 35 | +/// See: https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates |
| 36 | +abstract class GeolocationCoordinates { |
| 37 | + /// The position's latitude in decimal degrees. |
| 38 | + double get latitude; |
| 39 | + |
| 40 | + /// The position's longitude in decimal degrees. |
| 41 | + double get longitude; |
| 42 | + |
| 43 | + /// The position's altitude in meters, relative to sea level. |
| 44 | + double? get altitude; |
| 45 | + |
| 46 | + /// The accuracy of the latitude and longitude properties, expressed in meters. |
| 47 | + double get accuracy; |
| 48 | + |
| 49 | + /// The accuracy of the altitude expressed in meters. |
| 50 | + double? get altitudeAccuracy; |
| 51 | + |
| 52 | + /// The direction in which the device is traveling, in degrees (0-360). |
| 53 | + /// |
| 54 | + /// This value is 0 if the device is stationary. |
| 55 | + double? get heading; |
| 56 | + |
| 57 | + /// The magnitude of the horizontal component of the device's velocity in m/s. |
| 58 | + double? get speed; |
| 59 | +} |
| 60 | + |
| 61 | +// --------------------------------------------------------------------------- |
| 62 | +// GeolocationPositionError |
| 63 | +// --------------------------------------------------------------------------- |
| 64 | + |
| 65 | +/// Represents the reason of an error occurring when using the geolocating device. |
| 66 | +/// |
| 67 | +/// See: https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError |
| 68 | +abstract class GeolocationPositionError { |
| 69 | + /// The error code. |
| 70 | + int get code; |
| 71 | + |
| 72 | + /// A human-readable error message. |
| 73 | + String get message; |
| 74 | + |
| 75 | + /// The acquisition of the geolocation information failed because |
| 76 | + /// the page didn't have the permission to do it. |
| 77 | + static const int PERMISSION_DENIED = 1; |
| 78 | + |
| 79 | + /// The acquisition of the geolocation failed because at least one |
| 80 | + /// internal source of position returned an internal error. |
| 81 | + static const int POSITION_UNAVAILABLE = 2; |
| 82 | + |
| 83 | + /// The time allowed to acquire the geolocation, defined by |
| 84 | + /// [PositionOptions.timeout], was reached before the information was obtained. |
| 85 | + static const int TIMEOUT = 3; |
| 86 | +} |
| 87 | + |
| 88 | +// --------------------------------------------------------------------------- |
| 89 | +// PositionOptions |
| 90 | +// --------------------------------------------------------------------------- |
| 91 | + |
| 92 | +/// Options for methods [Geolocation.getCurrentPosition] and [Geolocation.watchPosition]. |
| 93 | +/// |
| 94 | +/// See: https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions |
| 95 | +class PositionOptions { |
| 96 | + /// A boolean value that indicates the application would like to receive the |
| 97 | + /// best possible results. |
| 98 | + /// |
| 99 | + /// If true and if the device is able to provide a more accurate position, |
| 100 | + /// it will do so. Note that this can result in slower response times or |
| 101 | + /// increased power consumption (with a GPS chip on a mobile device for example). |
| 102 | + /// On the other hand, if false, the device can take the liberty to save |
| 103 | + /// resources by responding more quickly and/or using less power. |
| 104 | + /// Default: false. |
| 105 | + final bool enableHighAccuracy; |
| 106 | + |
| 107 | + /// A positive long value representing the maximum length of time (in milliseconds) |
| 108 | + /// the device is allowed to take in order to return a position. |
| 109 | + /// |
| 110 | + /// The default value is Infinity, meaning that getCurrentPosition() won't return |
| 111 | + /// until the position is available. |
| 112 | + final int? timeout; |
| 113 | + |
| 114 | + /// A positive long value indicating the maximum age in milliseconds of a |
| 115 | + /// possible cached position that is acceptable to return. |
| 116 | + /// |
| 117 | + /// If set to 0, it means that the device cannot use a cached position and |
| 118 | + /// must attempt to retrieve the real current position. If set to Infinity |
| 119 | + /// the device must return a cached position regardless of its age. |
| 120 | + /// Default: 0. |
| 121 | + final int maximumAge; |
| 122 | + |
| 123 | + const PositionOptions({ |
| 124 | + this.enableHighAccuracy = false, |
| 125 | + this.timeout, |
| 126 | + this.maximumAge = 0, |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +// --------------------------------------------------------------------------- |
| 131 | +// Geolocation |
| 132 | +// --------------------------------------------------------------------------- |
| 133 | + |
| 134 | +/// The Geolocation interface represents an object able to programmatically |
| 135 | +/// obtain the position of the device. |
| 136 | +/// |
| 137 | +/// See: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation |
| 138 | +abstract class Geolocation { |
| 139 | + /// usage: getCurrentPosition(success, error, options) |
| 140 | + void getCurrentPosition( |
| 141 | + PositionCallback successCallback, [ |
| 142 | + PositionErrorCallback? errorCallback, |
| 143 | + PositionOptions? options, |
| 144 | + ]); |
| 145 | + |
| 146 | + /// usage: watchPosition(success, error, options) |
| 147 | + int watchPosition( |
| 148 | + PositionCallback successCallback, [ |
| 149 | + PositionErrorCallback? errorCallback, |
| 150 | + PositionOptions? options, |
| 151 | + ]); |
| 152 | + |
| 153 | + /// usage: clearWatch(id) |
| 154 | + void clearWatch(int watchId); |
| 155 | +} |
0 commit comments