当前位置:   article > 正文

鸿蒙应用开发学习:获取手机位置信息

鸿蒙应用开发学习:获取手机位置信息

一、前言

移动应用中经常需要获取设备的位置信息,因此在鸿蒙应用开发学习中,如何获取手机的位置信息是必修课。之前我想偷懒从别人那里复制黏贴代码,于是在百度上搜了一下,可能是我输入的关键字不对,结果没有找到想要的资料。于是我只能到官网上学习相关的开发文档(位置服务开发指南),自己摸索着做了,经过一番的学习,并在真机上测试,实现了获取手机位置信息的功能。特记之,已备忘。

二、实现方法

1. 首先在module.json5(位于entry/src/main文件夹下)文件中添加应用权限。

  1. "requestPermissions": [
  2. {
  3. "name": "ohos.permission.APPROXIMATELY_LOCATION"
  4. },
  5. {
  6. "name": "ohos.permission.LOCATION"
  7. }
  8. ]

2、在pages文件夹下通过“新建-page”创建一个ets文件,在文件中设置一个获取位置的按钮和用于显示位置信息的文本组件(具体代码见最后)

3、在这个ets文件导入 @ohos.geoLocationManager

import geoLocationManager from '@ohos.geoLocationManager'

4、实例化LocationRequest对象,用于告知系统该向应用提供何种类型的位置服务,以及位置结果上报的频率。我看了开发文档后,选择了方式二中的代码。

  1. let requestInfo = {
  2. 'priority': geoLocationManager.LocationRequestPriority.ACCURACY,
  3. 'timeInterval': 0,
  4. 'distanceInterval': 0,
  5. 'maxAccuracy': 0
  6. };

5、创建一个函数,这个函数通过点击界面中获取位置按钮来执行。函数实现以下功能:

(1)实例化Callback对象,用于向系统提供位置上报的途径。

(2)启动定位。

(3)获取系统缓存的最近一次历史定位结果。

(4)结束定位。

注:函数中的几个this开头的变量在主程序中以@state方法修饰,用于在文本控件中显示获取到的位置信息。

  1. getLocation() {
  2. let locationChange = (location) => {
  3. console.log('locationChanger: data: ' + JSON.stringify(location));
  4. };
  5. geoLocationManager.on('locationChange', requestInfo, locationChange);
  6. try {
  7. let location = geoLocationManager.getLastLocation();
  8. this.mLatitude = location.latitude.toString();
  9. this.mLongitude = location.longitude.toString();
  10. this.mAltitude = location.altitude.toString();
  11. this.mAccuracy = location.accuracy.toString();
  12. this.mSpeed = location.speed.toString();
  13. this.mTimeStamp = location.timeStamp.toString();
  14. this.mDirection = location.direction.toString();
  15. console.log("testTag", "获取到的位置信息:")
  16. console.log("testTag", "纬度latitude " + this.mLatitude)
  17. console.log("testTag", "经度longitude " + this.mLongitude)
  18. console.log("testTag", "海拔(米)altitude " + this.mAltitude)
  19. console.log("testTag", "精度(米)accuracy " + this.mAccuracy)
  20. console.log("testTag", "速度(米/秒)speed " + this.mSpeed)
  21. console.log("testTag", "时间戳timeStamp " + this.mTimeStamp)
  22. console.log("testTag", "方向direction " + this.mDirection)
  23. } catch (err) {
  24. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  25. }
  26. geoLocationManager.off('locationChange', locationChange);
  27. }

6、真机调试,安装了APP后,需要进入手机的“设置-应用和服务-应用管理”,找到安装的APP,手动将位置信息权限打开。(因为我还没有找到,自动开启权限的方式,只能手动开启)。

7、运行APP,进入手机定位页面,点击“获取位置”按钮,界面显示出了获取到的相关信息。

三、源代码

最后上我写的ets文件源代码。

  1. import geoLocationManager from '@ohos.geoLocationManager'
  2. let requestInfo = {
  3. 'priority': geoLocationManager.LocationRequestPriority.ACCURACY,
  4. 'timeInterval': 0,
  5. 'distanceInterval': 0,
  6. 'maxAccuracy': 0
  7. };
  8. @Entry
  9. @Component
  10. struct LocationPage {
  11. @State mLatitude: string = '' // 经度
  12. @State mLongitude: string = '' // 纬度
  13. @State mAltitude: string = '' // 海拔(米)
  14. @State mAccuracy: string = '' // 精度(米)
  15. @State mSpeed: string = '' //速度(米/秒)
  16. @State mTimeStamp: string = '' // 时间戳
  17. @State mDirection: string = '' // 方向
  18. build() {
  19. Column() {
  20. Button("获取位置")
  21. .width(100)
  22. .backgroundColor($r('app.color.button_bgColor_lightBlue'))
  23. .margin({ top: 50, bottom: 20 })
  24. .onClick(() => {
  25. this.getLocation()
  26. })
  27. Text('当前位置')
  28. .fontSize(24)
  29. Grid() {
  30. GridItem() {
  31. Text('经度:')
  32. }
  33. GridItem() {
  34. Text(this.mLatitude)
  35. }
  36. GridItem() {
  37. Text('纬度:')
  38. }
  39. GridItem() {
  40. Text(this.mLongitude)
  41. }
  42. GridItem() {
  43. Text('海拔:')
  44. }
  45. GridItem() {
  46. Text(this.mAltitude)
  47. }
  48. GridItem() {
  49. Text('精度:')
  50. }
  51. GridItem() {
  52. Text(this.mAccuracy)
  53. }
  54. GridItem() {
  55. Text('速度:')
  56. }
  57. GridItem() {
  58. Text(this.mSpeed)
  59. }
  60. GridItem() {
  61. Text('时间:')
  62. }
  63. GridItem() {
  64. Text(this.mSpeed)
  65. }
  66. GridItem() {
  67. Text('方向:')
  68. }
  69. GridItem() {
  70. Text(this.mDirection)
  71. }
  72. }
  73. .columnsTemplate('1fr 4fr')
  74. .rowsGap(15)
  75. .padding(10)
  76. .width('90%')
  77. }
  78. .width('100%')
  79. .backgroundColor('#EAEAEA')
  80. .padding(10)
  81. }
  82. // 获取手机当前位置
  83. getLocation() {
  84. let locationChange = (location) => {
  85. console.log('locationChanger: data: ' + JSON.stringify(location));
  86. };
  87. geoLocationManager.on('locationChange', requestInfo, locationChange);
  88. try {
  89. let location = geoLocationManager.getLastLocation();
  90. this.mLatitude = location.latitude.toString();
  91. this.mLongitude = location.longitude.toString();
  92. this.mAltitude = location.altitude.toString();
  93. this.mAccuracy = location.accuracy.toString();
  94. this.mSpeed = location.speed.toString();
  95. this.mTimeStamp = location.timeStamp.toString();
  96. this.mDirection = location.direction.toString();
  97. console.log("testTag", "获取到的位置信息:")
  98. console.log("testTag", "纬度latitude " + this.mLatitude)
  99. console.log("testTag", "经度longitude " + this.mLongitude)
  100. console.log("testTag", "海拔(米)altitude " + this.mAltitude)
  101. console.log("testTag", "精度(米)accuracy " + this.mAccuracy)
  102. console.log("testTag", "速度(米/秒)speed " + this.mSpeed)
  103. console.log("testTag", "时间戳timeStamp " + this.mTimeStamp)
  104. console.log("testTag", "方向direction " + this.mDirection)
  105. } catch (err) {
  106. console.error("errCode:" + err.code + ",errMessage:" + err.message);
  107. }
  108. geoLocationManager.off('locationChange', locationChange);
  109. }
  110. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/45745
推荐阅读
相关标签
  

闽ICP备14008679号