> 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/react-native/location.md).

# 位置

### 检测位置变化

**SDK 钩子：** `useGeolocation`

`useGeolocation`是一个返回设备位置信息的钩子。位置变化时，值也会变化。它会利用 GPS 信息检测当前位置，并随着用户移动自动更新。例如，可用于在地图服务中显示用户当前位置，或在配送应用中追踪实时移动路线。你还可以调整位置信息的精度和更新周期，在尽量减少电池消耗的同时，保持所需级别的精度。

**签名**

```typescript
function useGeolocation({ accuracy, distanceInterval, timeInterval }: UseGeolocationOptions): Location | null;
```

**参数**

* **options** · 必需 · `UseGeolocationOptions`

  用于检测位置信息的配置对象。

  * **options.accuracy** · `Accuracy`

    设置位置精度。 `Accuracy.Lowest`: 误差范围在 3KM 以内， `Accuracy.Low`: 误差范围在 1KM 以内， `Accuracy.Balanced`: 误差范围在几百米以内， `Accuracy.High`: 误差范围在 10M 以内， `Accuracy.Highest`: 最高精度， `Accuracy.BestForNavigation`: 用于导航的最高精度
  * **options.timeInterval** · `number`

    位置信息更新的最小周期，单位是毫秒(ms)。这个值会设置位置更新发生的最短间隔，但会受到系统或环境影响，可能会以比指定周期更长的间隔更新。
  * **options.distanceInterval** · `number`

    以米(m)为单位设置位置变化距离。

**返回值**

* Location | null

  返回包含设备位置信息的对象。详情请见 [Location](/documentation/api-and-sdk-zh/common/permission/location.md)。

**示例**

**在屏幕上显示位置信息**

```tsx
import React, { useState, useCallback } from 'react';
import { View, Text } from 'react-native';
import { useGeolocation, Accuracy } from '@apps-in-toss/framework';

// 检测位置变化
function LocationWatcher() {
  const location = useGeolocation({
    accuracy: Accuracy.Balanced,
    distanceInterval: 10,
    timeInterval: 1000,
  });

  if (location == null) {
    return <Text>正在获取位置信息...</Text>;
  }

  return (
    <View>
      <Text>
        位置信息：{location.latitude}，{location.longitude}
      </Text>
    </View>
  );
}
```

**体验示例应用**

[apps-in-toss-examples](https://github.com/toss/apps-in-toss-examples) 在仓库中 [with-location-tracking](https://github.com/toss/apps-in-toss-examples/tree/main/with-location-tracking) 下载代码，或扫描下方二维码亲自体验。

二维码链接：intoss\://with-location-tracking


---

# 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/react-native/location.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.
