> For the complete documentation index, see [llms.txt](https://developers-apps-in-toss.toss.im/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/device/device.subscribelocation.md).

# Device.subscribeLocation

> 函数式 API `startUpdateLocation`也可以这样使用同样的功能。

### 功能说明

持续检测设备位置的变化，每当位置发生变化时 `onEvent` 会执行回调。需要通过返回的 cleanup 函数取消订阅。 `timeInterval`由于系统影响，可能会比指定值更长。

如果没有位置权限， `onError` 在回调中 `StartUpdateLocationPermissionError`(`GetCurrentLocationPermissionError`会传入与其相同的类） `Device.subscribeLocation.getPermission()` / `Device.subscribeLocation.openPermissionDialog()`可以用来确认并请求权限。

### 类型

```ts
Device.subscribeLocation(params: StartUpdateLocationEventParams): () => void;
```

**Params**

```ts
type StartUpdateLocationEventParams = {
  onEvent: (location: Location) => void;
  onError: (error: unknown) => void;
  options: {
    accuracy: Accuracy;
    /** 位置更新的最小周期（ms）。 */
    timeInterval: number;
    /** 检测位置变化的距离（m）。 */
    distanceInterval: number;
  };
};
```

**Response**

用于取消订阅的 cleanup 函数（`() => void`）作为返回值。

### 错误

错误是 `onError` 会作为类实例传递给回调 — `error instanceof StartUpdateLocationPermissionError`来识别。该类可在 `@apps-in-toss/web-framework`中导入。

| 错误类                                  | 说明                                                                    |
| ------------------------------------ | --------------------------------------------------------------------- |
| `StartUpdateLocationPermissionError` | 表示位置更新权限被拒绝。 `Device.subscribeLocation.openPermissionDialog()`可以重新请求。 |

### 示例代码

```js
import {
  Accuracy,
  Device,
  StartUpdateLocationPermissionError,
} from "@apps-in-toss/web-framework";

const cleanup = Device.subscribeLocation({
  options: {
    accuracy: Accuracy.Balanced,
    timeInterval: 3000,
    distanceInterval: 10,
  },
  onEvent: (location) => {
    console.log(location.coords.latitude, location.coords.longitude);
  },
  onError: (error) => {
    if (error instanceof StartUpdateLocationPermissionError) {
      console.warn("没有位置权限。");
      return;
    }
    console.error(error);
  },
});

// 取消订阅
cleanup();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/device/device.subscribelocation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
