首页 > 解决方案 > 地理定位在 Ionic 4 Android 中不起作用

问题描述

为了获取 Android 设备中的当前位置,我使用了 Ionic 4 Geolocation插件并按照文档中给出的步骤进行操作。在运行 Ionic CLI 命令 ionic cordova run android --l --c 时,它会启动应用程序并提示位置权限对话框。但它会抛出一个错误,如getCurrentPosition() and watchPosition() are deprecated on insecure origins. To use this feature, you should consider switching your application to a secure origin.

{
   PositionErrorcode: 1message: "Only secure origins are allowed ."__proto__: PositionError
   home.page.ts:37Error getting location PositionError
}

同样在阅读了论坛中的博客和帖子之后,我只是使用了 cli 命令ionic cordova run android(无需重新加载)。在这种情况下,应用程序没有加载,因为源没有正确加载。

Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///polyfills.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///styles.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///cordova.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///main.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///assets/icon/favicon.png Failed to load resource: net::ERR_FILE_NOT_FOUND

我还尝试使用模拟器扩展控制在模拟器中设置 gps 位置。在真实设备中尝试过,它也有这样的问题。

标签: ionic-frameworkgeolocationandroid-emulatorionic-nativeionic4

解决方案


              this.geolocation.getCurrentPosition().then((resp) => {
                console.log(resp);
                // resp.coords.latitude
                // resp.coords.longitude
            }).catch((error) => {
                console.log('Error getting location', error);
            });

我的问题是打了两次电话。

当你反复调用时,结果可能不会到来。确保调用该函数一次。


推荐阅读