Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap/Cordova geolocation not working on Android

Tags:

I'm having a trouble to get Geolocation working on Android in both emulator (even when I geo fix over telnet) and on device. Works on iOS, WP8 and in the browser.

When I ask device for location using the following code, I always get an error (in my case custom Retrieving your position failed for unknown reason. with null both error code and error message).

Related code:

    successHandler = (position) ->
      resolve App.Location.create
        lat: position.coords.latitude
        lng: position.coords.longitude

    errorHandler = (error) ->
      error = switch error.code
        when 1
          App.LocationError.create
            message: 'You haven\'t shared your location.'
        when 2
          App.LocationError.create
            message: 'Couldn\'t detect your current location.'
        when 3
          App.LocationError.create
            message: 'Retrieving your position timeouted.'
        else
          App.LocationError.create
            message: 'Retrieving your position failed for unknown reason. Error code: ' + error.code + '. Error message: ' + error.message

      reject(error)

    options =
      maximumAge: Infinity # I also tried with 0
      timeout: 60000
      enableHighAccuracy: true
    navigator.geolocation.getCurrentPosition(successHandler, errorHandler, options)

platforms/android/AndroidManifest.xml

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

www/config.xml (just in case)

<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>

Using Cordova 3.1.0. Testing on Android 4.2. Plugin installed. Cordova.js included in index.html (other plugins like InAppBrowser are working fine).

$ cordova plugins ls
[ 'org.apache.cordova.console',
'org.apache.cordova.device',
'org.apache.cordova.dialogs',
'org.apache.cordova.geolocation',
'org.apache.cordova.inappbrowser',
'org.apache.cordova.vibration' ]

I'm clueless. Am I missing something?

like image 480
Kreeki Avatar asked Oct 21 '13 10:10

Kreeki


People also ask

What is geolocation watchPosition?

The Geolocation method watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function.


2 Answers

Try this,

Delete the geolocation plugin

cordova plugin rm org.apache.cordova.geolocation

However, ensure that AndroidManifest.xml and config.xml properties remains as such

(in app/res/xml/config.xml)
<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>


(in app/AndroidManifest.xml)
<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" />

And for iOS in config.xml

<feature name="Geolocation">
    <param name="ios-package" value="CDVLocation" />
</feature>

Then, build the application using CLI and run the application on device/emulator. You would find that you now get Error code as well as the message onError for getlocation.

MORE INFO: Even after doing the above steps you might get the timeout message consistently even after you setting {timeout:10000}. The solution for this is quite funny, just switch off your device and turn it on and run the application again. You should be surprised to see that working, just like me!

like image 45
Dipin Krishnan Avatar answered Dec 18 '22 14:12

Dipin Krishnan


Try options this way to see if it works for you as it has for me:

var options = { enableHighAccuracy: true };

navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
like image 138
ASP Force Avatar answered Dec 18 '22 13:12

ASP Force