ionic-framework - 地理定位在 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 位置。在真实设备中尝试过,它也有这样的问题。
解决方案
this.geolocation.getCurrentPosition().then((resp) => {
console.log(resp);
// resp.coords.latitude
// resp.coords.longitude
}).catch((error) => {
console.log('Error getting location', error);
});
我的问题是打了两次电话。
当你反复调用时,结果可能不会到来。确保调用该函数一次。
推荐阅读
- javascript - 更漂亮的规则最佳实践
- excel - 使用 Excel VBA 从列表中查找最大日期
- python - 优化nuitka编译速度?
- azure - 如何使用 ARM 模板创建 Azure DigitalTwin 模型、关系和孪生?
- javascript - 如何将脚本标签中的表的值添加到id = #show的html的表中?
- ios - 如何在苹果示例应用程序“Fruta”中打开成分卡时重新创建美丽的过渡
- highcharts - 如何更改面积图的边框?
- ios - 如何呈现视图而不将其嵌入到 SwiftUI 中的当前导航流中?
- c++ - 如何在自定义绘图 QT C++ 中显示所有 x 轴值
- python - 我想将我制作的函数包装在一个函数中,因此它是一段代码。我怎么做?