HarmonyOS 鸿蒙Next 关于'@system.geolocation'的海拔高度无法获取?
HarmonyOS 鸿蒙Next 关于’@system.geolocation’的海拔高度无法获取?
在DEV ECO STUDIO 3.1.1中:
getLocation() {
console.log(‘hello test getLoc’)
geolocation.getLocation({
success: function(data) {
console.info(‘success get location data. time:’ + data.time);
console.info(‘success get location data. latitude:’ + data.latitude);
console.info(‘success get location data. longtitude:’ + data.longitude);
console.info(‘success get location data. altitude:’ + data.altitude);
console.info(‘success get location data. accuracy:’ + data.accuracy);
},
fail: function(data, code) {
console.info(‘fail to get location. code:’ + code + ‘, data:’ + data);
}
});
}
在previewer log里看到:
I: success get location data. time:1735744609
I: success get location data. latitude:39.914417
I: success get location data. longtitude:116.39647
I: success get location data. altitude:undefined
I: success get location data. accuracy:20
1.请问为何data. altitude:undefined? 哪里用错了吗?
2.和设备相关吗?如果相关FIT3支持吗?
3.在DEV ECO Studio 5中,好像不支持这种操作了,但关于新的文档和demo代码也没怎么找到,可否给些参考?
更多关于HarmonyOS 鸿蒙Next 关于'@system.geolocation'的海拔高度无法获取?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
你好, geolocation.getLocation已经淘汰。现在使用 geoLocationManager.getCurrentLocation获取
import { geoLocationManager } from '[@kit](/user/kit).LocationKit';
import { BusinessError } from '[@kit](/user/kit).BasicServicesKit';
// 方式一:使用CurrentLocationRequest作为入参
let requestInfo:geoLocationManager.CurrentLocationRequest = {'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX, 'scenario': geoLocationManager.LocationRequestScenario.UNSET,'maxAccuracy': 0};
try {
geoLocationManager.getCurrentLocation(requestInfo).then((result) => {
console.info('current location: ' + JSON.stringify(result));
})
.catch((error:BusinessError) => {
console.error('promise, getCurrentLocation: error=' + JSON.stringify(error));
});
} catch (err) {
console.error("errCode:" + err.code + ", message:" + err.message);
}
// 方式二:使用SingleLocationRequest作为入参
let request:geoLocationManager.SingleLocationRequest = {'locatingTimeoutMs': 10000, 'locatingPriority': geoLocationManager.LocatingPriority.PRIORITY_ACCURACY};
try {
geoLocationManager.getCurrentLocation(request).then((result) => {
console.info('current location: ' + JSON.stringify(result));
})
.catch((error:BusinessError) => {
console.error('promise, getCurrentLocation: error=' + JSON.stringify(error));
});
} catch (err) {
console.error("errCode:" + err.code + ", message:" + err.message);
}
更多关于HarmonyOS 鸿蒙Next 关于'@system.geolocation'的海拔高度无法获取?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
谢谢回复,好像有问题啊。。。。 我用的是Dev ECO studio 5, 新建的是lite wearable project,在index.js里用您提供的代码,build时报错: //方式一报错: > hvigor Finished :entry:default@LegacyHookCompileResource… after 1 ms > hvigor Finished :entry:default@CacheNativeLibs… after 7 ms > hvigor ERROR: Failed :entry:default@LegacyCompileLiteJS… > hvigor ERROR: Tools execution failed. Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: D:\Project\LiteWearableTESTnewAPI\entry\src\main\js\MainAbility\pages\index\index.js: Missing semicolon. (4:15)
2 | import { BusinessError } from ‘@kit.BasicServicesKit’; 3 | > 4 | let requestInfo:geoLocationManager.CurrentLocationRequest = {‘priority’: geoLocationManager.LocationRequestPriority.FIRST_FIX, ‘scenario’: geoLocationManager.LocationRequestScenario.UNSET,‘maxAccuracy’: 0}; | ^ 5 | try { 6 | geoLocationManager.getCurrentLocation(requestInfo).then((result) => { 7 | console.info('current location: ’ + JSON.stringify(result));
Detail: Please check the message from tools. > hvigor ERROR: BUILD FAILED in 15 s 274 ms
Process finished with exit code -1
//方式二报错: > hvigor Finished :entry:default@CacheNativeLibs… after 11 ms > hvigor ERROR: Failed :entry:default@LegacyCompileLiteJS… > hvigor ERROR: Tools execution failed. Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: D:\Project\LiteWearableTESTnewAPI\entry\src\main\js\MainAbility\pages\index\index.js: Missing semicolon. (18:11)
16 | // 鏂瑰紡浜岋細浣跨敤SingleLocationRequest浣滀负鍏ュ弬 17 | > 18 | let request:geoLocationManager.SingleLocationRequest = {‘locatingTimeoutMs’: 10000, ‘locatingPriority’: geoLocationManager.LocatingPriority.PRIORITY_ACCURACY}; | ^ 19 | 20 | try { 21 |
Detail: Please check the message from tools. > hvigor ERROR: BUILD FAILED in 15 s 150 ms
您的代码是不是不支持lite wearable的设备或API?
在HarmonyOS 鸿蒙Next系统中,关于@system.geolocation
的海拔高度无法获取的问题,通常与以下几个因素有关:
-
设备硬件支持:首先确认你的设备是否具备获取海拔高度的硬件支持,如气压计等。部分设备可能仅支持基本的经纬度定位,而不支持海拔高度的测量。
-
API权限与调用:确保你的应用已正确申请并获得了地理位置权限,包括可能涉及的高度信息权限。同时,检查
@system.geolocation
API的调用方式是否正确,包括参数设置等。 -
系统与环境因素:系统版本、地理位置、天气条件等都可能影响海拔高度的测量精度和可用性。例如,在封闭空间或极端天气下,海拔高度数据可能不准确或无法获取。
-
API限制:
@system.geolocation
API本身可能存在获取海拔高度的限制或特定条件。查阅最新的HarmonyOS开发者文档,了解API的具体使用说明和限制。
如果以上因素均确认无误,但问题依旧存在,可能是系统层面的bug或特定设备的兼容性问题。此时,建议直接联系官网客服进行进一步的排查和解决。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html