• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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```