Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova Geolocation plugin returning empty position object on 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.

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.

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.

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

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

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?

like image 833
Flock Dawson Avatar asked Oct 01 '15 07:10

Flock Dawson


2 Answers

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 works fine without above adjustments, but as my app is a Phonegap application, I always apply the above.

like image 56
Flock Dawson Avatar answered Oct 22 '22 16:10

Flock Dawson


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.

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.

like image 43
Nick Bedford Avatar answered Oct 22 '22 16:10

Nick Bedford