Skip to content

Commit a451ffd

Browse files
authored
Merge pull request #657 from Telegram-Mini-Apps/docs/location-manager
Add location manager documentation
2 parents 45df6d8 + 8e392b4 commit a451ffd

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

.changeset/tame-otters-sort.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@telegram-apps/sdk": minor
3+
---
4+
5+
Implement `locationManager.isSupported()`.

apps/docs/.vitepress/packages.ts

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export const packagesLinksGenerator = (prefix: string = '') => {
138138
scope('haptic-feedback'),
139139
scope('init-data'),
140140
scope('invoice'),
141+
scope('location-manager'),
141142
scope('main-button'),
142143
scope('mini-app'),
143144
scope('popup'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
:::

packages/sdk/src/scopes/components/location-manager/exports.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export {
1212
requestLocation,
1313
mountPromise as locationManagerMountPromise,
1414
isAvailable as isLocationManagerAvailable,
15+
isSupported as isLocationManagerSupported,
1516
openSettings as openLocationManagerSettings,
1617
unmount as unmountLocationManager,
1718
} from './exports.variable.js';

packages/sdk/src/scopes/components/location-manager/exports.variable.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export {
1212
requestLocation,
1313
mountPromise,
1414
isAvailable,
15+
isSupported,
1516
openSettings,
1617
unmount,
1718
} from './location-manager.js';

packages/sdk/src/scopes/components/location-manager/location-manager.ts

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { defineNonConcurrentFn } from '@/scopes/defineNonConcurrentFn.js';
1313
import { signalCancel } from '@/scopes/signalCancel.js';
1414
import type { AsyncOptions } from '@/types.js';
1515
import { createComputed, createSignal } from '@/signals-registry.js';
16+
import { createIsSupported } from '@/scopes/createIsSupported.js';
1617

1718
const COMPONENT_NAME = 'locationManager';
1819
const CHECK_LOCATION_METHOD = 'web_app_check_location';
@@ -50,6 +51,11 @@ function fromState<K extends keyof State>(key: K): Computed<State[K]> {
5051
return createComputed(() => state()[key]);
5152
}
5253

54+
/**
55+
* Signal indicating whether the location data tracking is currently supported.
56+
*/
57+
export const isSupported = createIsSupported(CHECK_LOCATION_METHOD);
58+
5359
/**
5460
* Signal indicating whether the location data tracking is currently available.
5561
*/

0 commit comments

Comments
 (0)