简体   繁体   English

Cordova Geolocation插件在Android上返回空位置对象

[英]Cordova Geolocation plugin returning empty position object on Android

I've had quite some issues with the Geolocation Cordova plugin (org.apache.cordova.geolocation). 我在Geolocation Cordova插件(org.apache.cordova.geolocation)上遇到了一些问题。 It works fine on iOS, but it doesn't at all on Android. 它在iOS上运行良好,但在Android上根本没有。

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. 据我了解,该插件曾经包含原生的Android代码,但在某些时候被删除了,因为它太错误/慢,本机Web HTML5实现更加稳定和快速。

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). 如果我使用仍然具有本机代码的最新插件版本(0.3.2),它确实有效(但很慢,实际上并非总是如此)。 But when it does return, the position object is always populated. 但是当它确实返回时,总是填充位置对象。

If I use the latest plugin version (1.0.1), the getCurrentPosition() immediately returns with an empty object ({}). 如果我使用最新的插件版本(1.0.1),getCurrentPosition()会立即返回一个空对象({})。 It does not throw an error. 它不会引发错误。

If I remove the plugin completely, and add the permissions manually to the Android project: 如果我完全删除插件,并手动将权限添加到Android项目:

<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" />

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. Android控制台中不显示任何错误。 Any thoughts? 有什么想法吗?

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(). 显然,getCurrentPosition()函数在Android中返回一个“特殊”对象,在使用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 works fine without above adjustments, but as my app is a Phonegap application, I always apply the above. iOS工作正常,没有上述调整,但由于我的应用程序是Phonegap应用程序,我总是应用上述。

The Geolocation object passed to the callbacks in navigator.geolocation.getCurrentLocation() contains two prototype getters coords and timestamp , which means that hasOwnProperty will return false, which I assume that JSON.stringify excludes. 传递给navigator.geolocation.getCurrentLocation()中的回调的Geolocation对象包含两个原型 getter coordstimestamp ,这意味着hasOwnProperty将返回false,我假设JSON.stringify排除。

When logging all keys on the object I get the following: 记录对象上的所有键时,我得到以下内容:

[object Geoposition]
  .coords [object Coordinates]
     .latitude
     .longitude
     .altitude
     .accuracy 
     .altitudeAccuracy
     .heading
     .speed
  .timestamp

Never the less, these values are valid when accessed normally. 从不如此,这些值在正常访问时有效。

使用Angular,您可以使用以下方法解决此问题:

location = angular.copy(location)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM