【问题标题】:Geolocation is not working in Ionic 4 Android地理定位在 Ionic 4 Android 中不起作用
【发布时间】:2019-07-01 09:43:18
【问题描述】:

为了获取 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-framework geolocation android-emulator ionic-native ionic4


    【解决方案1】:
                  this.geolocation.getCurrentPosition().then((resp) => {
                    console.log(resp);
                    // resp.coords.latitude
                    // resp.coords.longitude
                }).catch((error) => {
                    console.log('Error getting location', error);
                });
    

    我的问题是调用两次

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

    【讨论】:

      【解决方案2】:

      在两个部分中,我会尽力提供帮助,我遇到了同样的两个问题,我解释了我是如何设法“修复”它们的,也许它也对你有用。

      (1)关于这个错误:

      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
      

      老实说,我不确定到底是什么原因造成的,我仍在调查根本原因,因为我遇到了这个问题,以防万一。奇怪的是,我在 Android 上遇到了这个问题,但在 iOS 平台上却没有。为了解决这个问题,我所做的是清除所有插件并重新安装它们:

      // Inside your app's folder
      $ rm -rf plugins 
      // Just in case (I didn't do it, but who knows!) remove and add the platform again
      $ ionic cordova platform remove android
      $ ionic cordova platform add android
      

      之后这些错误就消失了。

      (2) 关于地理位置

      是的,确实如此,我阅读了有关 HTTP/HTTPs 的文档,并且在通过浏览器使用时也收到了警告。 我没有让 Geolocation 在设备(iOS 和 Android)上工作,在编写完全符合 Ionic 文档所说的代码后,它根本没有得到任何生命信号。我以为是因为Chrome 50 上的 Geolocation (HTTP/HTTPS) 的问题。

      所以,我发现在设备(Android 和 iOS)上,Geolocation 工作正常,如果在平台准备好后启动它们(或使用 Timeout 参数,作为替代)。 使设备获取用户位置的代码如下:

         this.platform.ready().then(() => {
              this.geolocation.getCurrentPosition().then((loc) => {
                    userLatitude  = loc.coords.latitude;
                    userLongitude = loc.coords.longitude;
                    map.flyTo({userLatitude:userLongitude}, MAX_ZOOM - 2, ZOOM_PAN_OPTIONS);
              }).catch((error) => {
                  this.utils.error('Error getting location: ' + error.message);
              }); 
         };
      

      我没有测试,但其他替代方法可能是在方法上使用Timeout 参数:

      this.geolocation.getCurrentPosition({ timeout: 30000 }).then((loc) => { ...
      

      我认为有很多变量,就所有上下文而言,可以使这个工作或不工作。就我而言,它是这样发生的。

      【讨论】:

        【解决方案3】:

        不推荐使用不安全 (HTTP) 上下文访问用户的位置。 见https://www.chromium.org/Home/chromium-security/prefer-secure-origins-for-powerful-new-features

        这只是使用实时重载时的问题。

        这将工作ionic cordova build android

        【讨论】:

        • 它没有用。正如我的问题中提到的,如果我执行命令 ionic cordova run android / ionic cordova build android,它会抛出标题错误,无法加载资源:net::ERR_FILE_NOT_FOUND。这个特殊问题不仅适用于这个项目。升级到 Ionic 4 后,我在所有项目中都面临这个问题。如果我添加重新加载选项,同样的工作。谢谢。
        • 那没有帮助。从 index.html 中删除 会给我一个错误“错误:未设置基本 href。请为 APP_BASE_HREF 令牌提供值或向文档添加基本元素”。按照链接中的指示更改配置对结果没有任何影响。
        猜你喜欢
        • 2018-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-05
        • 2019-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多