Cordoba Geolocation plugin that returns an empty position object on Android

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

As I understand it, the plugin used its own Android code, but at some point it was deleted because it was too buggy / slow, and the embedded implementation of HTML5 was much more stable and fast.

If I use the latest version of the plugin (0.3.2), which still has its own code, it works (but slowly and really, not always). But when it returns, the position object is always filled.

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

If I completely remove the plugin and manually add permissions 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 is happening. I just can't get it to work, but that doesn't make sense. The Android console does not display errors. Any thoughts?

+6
source share
3 answers

OK, after a long long debugging, I found a problem. Apparently, the getCurrentPosition () function returns a β€œspecial” object in Android that evaluates {} when using JSON.stringify (). If I brought the raw return object to the console, it turned out that it was not empty at all.

So, after ridiculous settings, I 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 additional settings, but since my application is a Phonegap application, I always apply the above.

+21
source

The Geolocation object passed to callbacks in navigator.geolocation.getCurrentLocation() contains two prototypes getters coords and timestamp , which means that hasOwnProperty will return false, which I assume JSON.stringify excludes.

When registering all the keys on the object, I get the following:

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

However, these values ​​are valid for regular access.

+2
source

With Angular, you can fix this with

 location = angular.copy(location) 
0
source

All Articles