<bdo id='tZKWO'></bdo><ul id='tZKWO'></ul>
  • <i id='tZKWO'><tr id='tZKWO'><dt id='tZKWO'><q id='tZKWO'><span id='tZKWO'><b id='tZKWO'><form id='tZKWO'><ins id='tZKWO'></ins><ul id='tZKWO'></ul><sub id='tZKWO'></sub></form><legend id='tZKWO'></legend><bdo id='tZKWO'><pre id='tZKWO'><center id='tZKWO'></center></pre></bdo></b><th id='tZKWO'></th></span></q></dt></tr></i><div id='tZKWO'><tfoot id='tZKWO'></tfoot><dl id='tZKWO'><fieldset id='tZKWO'></fieldset></dl></div>
  • <small id='tZKWO'></small><noframes id='tZKWO'>

  • <tfoot id='tZKWO'></tfoot>

        <legend id='tZKWO'><style id='tZKWO'><dir id='tZKWO'><q id='tZKWO'></q></dir></style></legend>
      1. Cordova Geolocation 插件在 Android 上返回空位置对象

        时间:2023-09-01

            1. <legend id='wj1TO'><style id='wj1TO'><dir id='wj1TO'><q id='wj1TO'></q></dir></style></legend>

                <tbody id='wj1TO'></tbody>
            2. <small id='wj1TO'></small><noframes id='wj1TO'>

              <i id='wj1TO'><tr id='wj1TO'><dt id='wj1TO'><q id='wj1TO'><span id='wj1TO'><b id='wj1TO'><form id='wj1TO'><ins id='wj1TO'></ins><ul id='wj1TO'></ul><sub id='wj1TO'></sub></form><legend id='wj1TO'></legend><bdo id='wj1TO'><pre id='wj1TO'><center id='wj1TO'></center></pre></bdo></b><th id='wj1TO'></th></span></q></dt></tr></i><div id='wj1TO'><tfoot id='wj1TO'></tfoot><dl id='wj1TO'><fieldset id='wj1TO'></fieldset></dl></div>

                <tfoot id='wj1TO'></tfoot>
                  <bdo id='wj1TO'></bdo><ul id='wj1TO'></ul>
                  本文介绍了Cordova Geolocation 插件在 Android 上返回空位置对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在 Geolocation Cordova 插件 (org.apache.cordova.geolocation) 上遇到了很多问题.它在 iOS 上运行良好,但在 Android 上根本不行.

                  I've had quite some issues with the Geolocation Cordova plugin (org.apache.cordova.geolocation). It works fine on iOS, but it doesn't at all on Android.

                  据我了解,该插件曾经包含原生 Android 代码,但在某些时候被删除了,因为它太错误/太慢了,而且原生 Web HTML5 实现更加稳定和快速.

                  As I understand, the plugin used to include native Android code, but this was removed at some point, because it was too buggy/slow and the native web HTML5 implementation was much more stable and fast.

                  如果我使用仍然具有本机代码的最新插件版本 (0.3.2),它确实可以工作(但速度很慢,实际上并非总是如此).但是当它返回时,位置对象总是被填充.

                  If I use the latest plugin version (0.3.2) which still has the native code, it does work (but slow and indeed, not always). But when it does return, the position object is always populated.

                  如果我使用最新的插件版本 (1.0.1),getCurrentPosition() 会立即返回一个空对象 ({}).它不会抛出错误.

                  If I use the latest plugin version (1.0.1), the getCurrentPosition() immediately returns with an empty object ({}). It does not throw an error.

                  如果我完全删除插件,并手动将权限添加到 Android 项目:

                  If I remove the plugin completely, and add the permissions manually to the Android project:

                  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
                  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
                  <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
                  

                  同样的事情也会发生.我只是无法让它工作,但这没有意义.Android 控制台中不会显示任何错误.有什么想法吗?

                  The same thing happens. I just can't get it to work, but it doesn't make sense. No errors are displayed in the Android console. Any thoughts?

                  推荐答案

                  好的,调试了好久,发现问题了.显然,getCurrentPosition() 函数在 Android 中返回一个特殊"对象,当使用 JSON.stringify() 时,它的计算结果为 {}.如果我将原始返回对象输出到控制台,结果证明它根本不是空的.

                  OK, after a long long time of debugging, I found the problem. Apparently, the getCurrentPosition() function returns a 'special' object in Android, which evaluates to {} when using JSON.stringify(). If I outputted the raw return object to the console, it turned out it wasn't empty at all.

                  因此,在荒谬的调整之后修复了我的代码:

                  So, following ridiculous adjustments fixed my code:

                  navigator.geolocation.getCurrentPosition(function (position) {
                      var positionObject = {};
                  
                      if ('coords' in position) {
                          positionObject.coords = {};
                  
                          if ('latitude' in position.coords) {
                              positionObject.coords.latitude = position.coords.latitude;
                          }
                          if ('longitude' in position.coords) {
                              positionObject.coords.longitude = position.coords.longitude;
                          }
                          if ('accuracy' in position.coords) {
                              positionObject.coords.accuracy = position.coords.accuracy;
                          }
                          if ('altitude' in position.coords) {
                              positionObject.coords.altitude = position.coords.altitude;
                          }
                          if ('altitudeAccuracy' in position.coords) {
                              positionObject.coords.altitudeAccuracy = position.coords.altitudeAccuracy;
                          }
                          if ('heading' in position.coords) {
                              positionObject.coords.heading = position.coords.heading;
                          }
                          if ('speed' in position.coords) {
                              positionObject.coords.speed = position.coords.speed;
                          }
                      }
                  
                      if ('timestamp' in position) {
                          positionObject.timestamp = position.timestamp;
                      }
                  
                      // Use the positionObject instead of the position 'object'
                      alert(JSON.stringify(positionObject));            
                  }
                  

                  iOS 无需上述调整即可正常工作,但由于我的应用程序是 Phonegap 应用程序,所以我总是应用上述内容.

                  iOS works fine without above adjustments, but as my app is a Phonegap application, I always apply the above.

                  这篇关于Cordova Geolocation 插件在 Android 上返回空位置对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Android 中获取位置管理器的最后一个已知 下一篇:美国所有州的 iOS 覆盖(MKPolygon)数据?

                  相关文章

                  最新文章

                  <tfoot id='FZAzp'></tfoot>
                  1. <i id='FZAzp'><tr id='FZAzp'><dt id='FZAzp'><q id='FZAzp'><span id='FZAzp'><b id='FZAzp'><form id='FZAzp'><ins id='FZAzp'></ins><ul id='FZAzp'></ul><sub id='FZAzp'></sub></form><legend id='FZAzp'></legend><bdo id='FZAzp'><pre id='FZAzp'><center id='FZAzp'></center></pre></bdo></b><th id='FZAzp'></th></span></q></dt></tr></i><div id='FZAzp'><tfoot id='FZAzp'></tfoot><dl id='FZAzp'><fieldset id='FZAzp'></fieldset></dl></div>
                  2. <small id='FZAzp'></small><noframes id='FZAzp'>

                        <bdo id='FZAzp'></bdo><ul id='FZAzp'></ul>
                    1. <legend id='FZAzp'><style id='FZAzp'><dir id='FZAzp'><q id='FZAzp'></q></dir></style></legend>