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?
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:
iOS works fine without above adjustments, but as my app is a Phonegap application, I always apply the above.
The
Geolocation
object passed to the callbacks innavigator.geolocation.getCurrentLocation()
contains two prototype getterscoords
andtimestamp
, which means thathasOwnProperty
will return false, which I assume thatJSON.stringify
excludes.When logging all keys on the object I get the following:
Never the less, these values are valid when accessed normally.
With Angular, you can fix this with :