• Home
Name Date Size #Lines LOC

..--

AppScope/06-May-2025-3633

entry/06-May-2025-851699

hvigor/06-May-2025-2322

position/06-May-2025-773619

screenshots/devices/zh/06-May-2025-

.gitignoreD06-May-202593 77

README.mdD06-May-20252.4 KiB3919

README_zh.mdD06-May-20253.5 KiB6648

build-profile.json5D06-May-20251.1 KiB4745

hvigorfile.jsD06-May-2025168 21

hvigorwD06-May-20252.1 KiB6228

hvigorw.batD06-May-20252 KiB7256

oh-package.json5D06-May-2025813 2625

ohosTest.mdD06-May-2025714 108

README.md

1# Geolocation
2
3### Introduction
4
5This sample shows how to use **geolocation** APIs to implement location services.
6
7In this example, we'll use [Geolocation](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-location-kit/js-apis-geolocation.md) APIs to obtain the longitude and latitude of the current location, and use [HTTP](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-network-kit/js-apis-http.md) APIs to obtain the city where the longitude and latitude are located. With the [AlphabetIndexer](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-arkui/arkui-ts/ts-container-alphabet-indexer.md) APIs, we can also quickly locate the container display area based on the logical structure.
8
9How to Use
10
111. Go to the home page and touch a popular city in the popular city list. The delivery address will be updated to the selected city. Alternatively, touch the alphabetic index bar on the right to quickly locate the desired city. If the city is divided into districts, you can click the desired district to update the delivery address to that district.
12
132. If your test device supports the GPS function, you can touch the location icon above the popular city list to locate the city where you settle. The city next to the location icon will be updated accordingly. Current city data is analog data.
14
15
16### Preview
17
18![](screenshots/devices/zh/position.png)
19
20### Required Permissions
21
22Using network sockets: [ohos.permission.INTERNET](https://gitee.com/openharmony/docs/blob/master/en/application-dev/security/AccessToken/permissions-for-all.md)
23
24Obtaining location information while running in the foreground: [ohos.permission.LOCATION](https://gitee.com/openharmony/docs/blob/master/en/application-dev/security/AccessToken/permissions-for-all-user.md)
25
26Obtaining location information while running in the background: [ohos.permission.LOCATION_IN_BACKGROUND](https://gitee.com/openharmony/docs/blob/master/en/application-dev/security/AccessToken/permissions-for-all-user.md)
27
28### Dependency
29
30N/A
31
32### Constraints
33
341. This sample can only be run on standard-system devices. To use GPS positioning, the test device must support the GPS function.
35
362. This sample demonstrates the stage model, which supports only the SDK of API version 9 (SDK version: 3.2.11.9).
37
383. DevEco Studio 3.1 Canary1 (Build Version: 3.1.0.100) must be used.
39

README_zh.md

1# 位置信息
2
3### 介绍
4
5本示例使用 [geolocation](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-location-kit/js-apis-geolocation.md) 实现获取当前位置的经纬度,然后通过 [http](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-network-kit/js-apis-http.md) 将经纬度作为请求参数,获取到该经纬度所在的城市。通过 [AlphabetIndexer](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/arkui-ts/ts-container-alphabet-indexer.md) 容器组件实现按逻辑结构快速定位容器显示区域。
6
7### 效果预览
8|首页|
9|--------|
10|![](screenshots/devices/zh/position.png)|
11
12使用说明
131. 进入主页,点击国内热门城市,配送地址会更新为选择的城市。点击右边字母索引条,可快速定位到想要选择的城市区域,点击该城市后若该城市还细化到区,继续点击该城市的区,配送地址会更新为城市/区,若未细化到区,则只选择城市。
142. 若测试机支持GPS,点击国内热门城市上面的定位图标,应用会获取本机所在经纬度,然后根据经纬度获取所在城市,定位图标后的城市会进行刷新,当前城市数据为模拟数据。
15
16### 工程目录
17```
18position/src/main/ets/
19|---components
20|   |---mock
21|   |   |---LocationMock.ts                         // 城市信息mock数据
22|   |---model
23|   |   |---data.ts                                 // AlphabetIndexer索引
24|   |---net
25|   |   |---HttpRequestResponse.ts                  // 网络请求
26|   |---pages
27|   |   |---Location.ts                             // 页面
28|   |---utils
29|   |   |---DataSource.ts                           // 懒加载数据格式
30|   |   |---Logger.ts                               // 日志工具
31```
32
33### 具体实现
34+ 本示例展示获取定位的功能在页面中直接调用,使用geolocation.on方法获取当前位置的经纬度,然后监听经纬度变化发送request请求获取对应城市信息,源码参考[Location.ets](position/src/main/ets/components/pages/Location.ets)。
35
36### 相关权限
37
38[ohos.permission.INTERNET](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissioninternet)
39
40[ohos.permission.LOCATION](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionlocation)
41
42[ohos.permission.LOCATION_IN_BACKGROUND](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionlocation_in_background)
43
44[ohos.permission.APPROXIMATELY_LOCATION](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionapproximately_location)
45
46### 依赖
47
48不涉及。
49
50### 约束与限制
51
521. 本示例仅支持标准系统上运行,支持设备:GPS定位功能仅支持部分机型。
532. 本示例为Stage模型,已适配API version 9版本SDK,版本号:3.2.11.9。
543. 本示例需要使用DevEco Studio 3.1 Beta2 (Build Version: 3.1.0.400 构建 2023年4月7日)及以上版本才可编译运行。
55
56### 下载
57
58如需单独下载本工程,执行如下命令:
59
60```
61git init
62git config core.sparsecheckout true
63echo code/BasicFeature/DeviceManagement/Location/ > .git/info/sparse-checkout
64git remote add origin https://gitee.com/openharmony/applications_app_samples.git
65git pull origin master
66```