定位
1.先申請地址權限(大致位置+精準位置)
module.json5文件
"requestPermissions": [{"name": "ohos.permission.INTERNET"
},{"name": "ohos.permission.LOCATION","reason": "$string:app_name","usedScene": {"abilities": ["EntryAbility"],"when": "always"}},{"name": "ohos.permission.APPROXIMATELY_LOCATION","reason": "$string:app_name","usedScene": {"abilities": ["EntryAbility"],"when": "always"}}
],
2.向用戶申請權限->獲取當前地理位置->根據經緯度獲取到具體地址
Button('位置').onClick(async () => {// 向用戶申請權限const manager = abilityAccessCtrl.createAtManager()const result = await manager.requestPermissionsFromUser(getContext(),["ohos.permission.LOCATION", "ohos.permission.APPROXIMATELY_LOCATION"])if (result.authResults[0] == 0 && result.authResults[1] == 0) {// 用戶授權成功,獲取當前地理位置const res = await geoLocationManager.getCurrentLocation({ maxAccuracy: 100 })const latitude = res.latitude // 緯度const longitude = res.longitude // 經度// 根據經緯度獲取到具體地址const address = await geoLocationManager.getAddressesFromLocation({ latitude, longitude })this.addressData.address = address[0].placeName || ''} else {AlertDialog.show({ message: "獲取失敗,沒有權限" })}})