HarmonyOS 鸿蒙Next API8使用geolocation获取位置失败

HarmonyOS 鸿蒙Next API8使用geolocation获取位置失败 使用geolocation.on方法和geolocation.getCurrentLocation方法获取当前位置信息都失败,代码如下:

// index.js

import hilog from '@ohos.hilog';
import featureAbility from '@ohos.ability.featureAbility';
import geolocation from '@ohos.geolocation';

export default {
    data: {
        title: "--"
    },
    OnInit() {
        requestPermissionFromUser();
    },
    updateLocation() {
        var requestInfo = {
            'scenario': 0x301, 'timeInterval': 0, 'distanceInterval': 0, 'maxAccuracy': 0
        };
        var locationChange = (location) => {
            console.log('locationChanger: data: ' + JSON.stringify(location));
        };
        geolocation.on('locationChange', requestInfo, locationChange);
    },
    getLocation() {
        var requestInfo = {'priority': 0x203, 'scenario': 0x300,'maxAccuracy': 0};
        var locationChange = (err, location) => {
            console.log('locationChanger: ' + err + 'data: ' + location);
        };
        geolocation.getCurrentLocation(requestInfo, locationChange);
        geolocation.getCurrentLocation(locationChange);
    },
}

/**
 * 向用户动态申请权限
 */
function requestPermissionFromUser() {
    hilog.info(0x0000, 'testTag', 'requestPermissionFromUser');
    var context = featureAbility.getContext()
    let array = ["ohos.permission.LOCATION"];
    // requestPermissionsFromUser 会判断权限的授权状态来决定是否唤起弹窗
    context.requestPermissionsFromUser(array, 1, (err, data) => {
        hilog.info(0x0000, 'testTag', "====>requestdata====> %{public}s", JSON.stringify(data));
        hilog.info(0x0000, 'testTag', "====>requesterrcode====> %{public}s", JSON.stringify(err.code));
    })
}

HML页面代码如下:

<div class="container">
    <text class="title">
        {{ title }}
    </text>
    <button class="button" @click="getLocation">
        Start Request Location
    </button>
</div>

使用geolocation.on获取位置后,报错信息如下:

09-19 20:21:20.772 20757-20819/com.example.jslocationdemo W 03900/Ace:  [jsi_group_js_bridge.cpp(TriggerModulePluginGetErrorCallback)-(2)] trigger Js reject callback function fail, callbackId:<private>
09-19 20:21:20.785 20757-20791/com.example.jslocationdemo I 01510/RemoteObject:  Java onRemoteRequest called
09-19 20:21:20.785 20757-20791/com.example.jslocationdemo E 01510/IPCJni:  OnRemoteRequest res:0
09-19 20:21:20.834 20757-20791/com.example.jslocationdemo I 02300/LocatorAdapter:  onLocationReport: Location: 30*****, 120***** alt=0.0 acc=36.0 speed=0.0 direction=0.0 timeStamp=1663590080825 timeSinceBoot=1906110721583
09-19 20:21:20.834 20757-20834/com.example.jslocationdemo I 02300/Locator:  stopLocating callback.
09-19 20:21:20.835 20757-20791/com.example.jslocationdemo I 03910/Ace:  PluginHandlersManager: <private>
09-19 20:21:20.835 20757-20819/com.example.jslocationdemo I 03B00/JSApp:  app Log: GroupMessenger parse json result
09-19 20:21:20.835 20757-20819/com.example.jslocationdemo I 03900/Ace:  [jsi_engine.cpp(PrintLog)-(2)] Ark debugger::JSPtHooks: PendingJobEntry
09-19 20:21:20.835 20757-20819/com.example.jslocationdemo I chatty    :  uid=10372(com.example.jslocationdemo) jsThread-1 identical 1 line
09-19 20:21:20.835 20757-20819/com.example.jslocationdemo I 03900/Ace:  [jsi_engine.cpp(PrintLog)-(2)] Ark debugger::JSPtHooks: PendingJobEntry
09-19 20:21:20.835 20757-20791/com.example.jslocationdemo I 02300/Locator:  stopLocating callback.

使用geolocation.getCurrentLocation获取位置后,报错信息如下:

Device info:OCE-AL50
Build info:OCE-AL50 3.0.0.117(C00E110R8P6)
Module name:com.example.jslocationdemo
Version:1.0.0
Pid:17430
Uid:10369
Lifetime: 38.000000s
Js-Engine: ark
page: pages/index/index.js
Error message: is not callable
Stacktrace:
    at anonymous (/mnt/disk/jenkins/ci/workspace/system_pipeline_release/phone_compile/component_code/hm_z/base/location/frameworks/js_plugin/ohos/geolocation/LocationPlugin.js:151:151)

更多关于HarmonyOS 鸿蒙Next API8使用geolocation获取位置失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

楼主,你好,已将问题反馈给开发,请耐心等待

更多关于HarmonyOS 鸿蒙Next API8使用geolocation获取位置失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对HarmonyOS鸿蒙Next API8使用geolocation获取位置失败的问题,可能的原因及解决方案如下:

  1. 权限问题

    • 确认应用是否已经声明并获得了地理位置权限。在manifest文件中添加必要的权限声明,并在运行时请求用户授权。
  2. 服务未启用

    • 检查设备的地理位置服务是否已启用。用户可能手动禁用了位置服务,需要在设备设置中确认。
  3. API调用错误

    • 确认geolocation API的使用方式是否正确。检查API调用的参数、回调函数的设置以及错误处理逻辑。
  4. 系统或软件版本问题

    • 确认鸿蒙系统版本是否支持geolocation API的当前使用方式。某些API可能在特定版本后才被引入或有所变更。
  5. 硬件故障

    • 排除设备硬件故障的可能性,如GPS模块损坏等。可以尝试在其他应用或设备上测试geolocation功能。
  6. 第三方库或依赖问题

    • 如果使用了第三方库来获取位置信息,检查该库是否与鸿蒙系统兼容,以及是否有已知的问题或更新。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部