|
| 1 | +# Location Manager |
| 2 | + |
| 3 | +The 💠[component](../scopes.md) responsible for location tracking functionality for Telegram Mini |
| 4 | +Apps. |
| 5 | + |
| 6 | +## Checking Support |
| 7 | + |
| 8 | +To check if location tracking is supported by the current Telegram Mini Apps version, use the |
| 9 | +`isSupported` signal: |
| 10 | + |
| 11 | +::: code-group |
| 12 | + |
| 13 | +```ts [Variable] |
| 14 | +import { locationManager } from '@telegram-apps/sdk'; |
| 15 | + |
| 16 | +locationManager.isSupported(); // boolean |
| 17 | +``` |
| 18 | + |
| 19 | +```ts [Functions] |
| 20 | +import { isLocationManagerSupported } from '@telegram-apps/sdk'; |
| 21 | + |
| 22 | +isLocationManagerSupported(); // boolean |
| 23 | +``` |
| 24 | + |
| 25 | +::: |
| 26 | + |
| 27 | +## Mounting |
| 28 | + |
| 29 | +Before using the component, it must be mounted. |
| 30 | + |
| 31 | +This process is asynchronous, as location manager settings need to be requested from the Telegram |
| 32 | +application. The `isMounting` signal will be set to `true` during the process and updated to `false` |
| 33 | +when complete. |
| 34 | + |
| 35 | +If mounting is successful, the `isMounted` signal will be set to `true`. If errors occur, |
| 36 | +the `mountError` signal will reflect the error. |
| 37 | + |
| 38 | +::: code-group |
| 39 | + |
| 40 | +```ts [Variable] |
| 41 | +if (locationManager.mount.isAvailable()) { |
| 42 | + try { |
| 43 | + const promise = locationManager.mount(); |
| 44 | + locationManager.isMounting(); // true |
| 45 | + await promise; |
| 46 | + locationManager.isMounting(); // false |
| 47 | + locationManager.isMounted(); // true |
| 48 | + } catch (err) { |
| 49 | + locationManager.mountError(); // equals "err" |
| 50 | + locationManager.isMounting(); // false |
| 51 | + locationManager.isMounted(); // false |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +```ts [Functions] |
| 57 | +import { |
| 58 | + mountLocationManager, |
| 59 | + isLocationManagerMounting, |
| 60 | + isLocationManagerMounted, |
| 61 | + locationManagerMountError, |
| 62 | +} from '@telegram-apps/sdk'; |
| 63 | + |
| 64 | +if (mountLocationManager.isAvailable()) { |
| 65 | + try { |
| 66 | + const promise = mountLocationManager(); |
| 67 | + isLocationManagerMounting(); // true |
| 68 | + await promise; |
| 69 | + isLocationManagerMounting(); // false |
| 70 | + isLocationManagerMounted(); // true |
| 71 | + } catch (err) { |
| 72 | + locationManagerMountError(); // equals "err" |
| 73 | + isLocationManagerMounting(); // false |
| 74 | + isLocationManagerMounted(); // false |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +::: |
| 80 | + |
| 81 | +To unmount, use the `unmount` method: |
| 82 | + |
| 83 | +::: code-group |
| 84 | + |
| 85 | +```ts [Variable] |
| 86 | +locationManager.unmount(); |
| 87 | +locationManager.isMounted(); // false |
| 88 | +``` |
| 89 | + |
| 90 | +```ts [Functions] |
| 91 | +import { unmountLocationManager, isLocationManagerMounted } from '@telegram-apps/sdk'; |
| 92 | + |
| 93 | +unmountLocationManager(); |
| 94 | +isLocationManagerMounted(); // false |
| 95 | +``` |
| 96 | + |
| 97 | +::: |
| 98 | + |
| 99 | +## Requesting Location |
| 100 | + |
| 101 | +To request the current location, use the `requestLocation` method. It returns a cancelable promise |
| 102 | +with an object, describing the current device location. |
| 103 | + |
| 104 | +The object contains the following numeric properties: |
| 105 | + |
| 106 | +| Property | Description | |
| 107 | +|---------------------|----------------------------------------------------------------------| |
| 108 | +| latitude | Latitude in degrees. | |
| 109 | +| longitude | Longitude in degrees. | |
| 110 | +| altitude | _Optional_. Altitude above sea level in meters. | |
| 111 | +| course | _Optional_. The direction the device is moving in degrees. | |
| 112 | +| speed | _Optional_. The speed of the device in m/s. | |
| 113 | +| horizontal_accuracy | _Optional_. Accuracy of the latitude and longitude values in meters. | |
| 114 | +| vertical_accuracy | _Optional_. Accuracy of the altitude value in meters. | |
| 115 | +| course_accuracy | _Optional_. Accuracy of the course value in degrees. | |
| 116 | +| speed_accuracy | _Optional_. Accuracy of the speed value in m/s. | |
| 117 | + |
| 118 | +::: code-group |
| 119 | + |
| 120 | +```ts [Variable] |
| 121 | +if (locationManager.requestLocation.isAvailable()) { |
| 122 | + const location = await locationManager.requestLocation(); |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +```ts [Functions] |
| 127 | +import { requestLocation } from '@telegram-apps/sdk'; |
| 128 | + |
| 129 | +if (requestLocation.isAvailable()) { |
| 130 | + const location = await requestLocation(); |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +::: |
| 135 | + |
| 136 | +## Opening Settings |
| 137 | + |
| 138 | +To open the location manager-related settings modal, use the `openSettings` method. This method can |
| 139 | +only be triggered in response to user interaction. |
| 140 | + |
| 141 | +::: code-group |
| 142 | + |
| 143 | +```ts [Variable] |
| 144 | +if (locationManager.openSettings.isAvailable()) { |
| 145 | + locationManager.openSettings(); |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +```ts [Functions] |
| 150 | +import { openLocationManagerSettings } from '@telegram-apps/sdk'; |
| 151 | + |
| 152 | +if (openLocationManagerSettings.isAvailable()) { |
| 153 | + openLocationManagerSettings(); |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +::: |
0 commit comments