Phonegap geolocation is not working on Android (Nexus device)

664 Views Asked by At

I know this question has been asked before, but I've tried all the proposed solutions and nothing seems to work!

I'm working on a mobile app built with Phonegap (+Angular but I don't think that's important). In this app I'm using the geolocation plugin. More specifically my config.xml looks like the following:

<?xml version="1.0" encoding="UTF-8" ?>
    <widget xmlns   = "http://www.w3.org/ns/widgets"
        xmlns:gap   = "http://phonegap.com/ns/1.0"
        id          = "......"
        version     = "1.0.0" >

    <name>.....</name>

    <preference name="permissions"                value="none"/>

    <preference name="orientation"                value="default" />
    <preference name="target-device"              value="universal" />
    <preference name="phonegap-version"           value="3.6.3" />

    <gap:plugin name="org.apache.cordova.geolocation" />
    <gap:plugin name="com.phonegap.plugins.PushPlugin" version="2.4.0" />
    <gap:plugin name="org.apache.cordova.network-information" />
    <gap:plugin name="org.apache.cordova.statusbar" />
    <gap:plugin name="org.apache.cordova.device" />
    <gap:plugin name="org.transistorsoft.cordova.background-geolocation"/>
</widget>

There are times that I want to get the current position of the user, so I'm doing it as follows:

navigator.geolocation.getCurrentPosition(function (position) {
    .....
}, function (error) {}, {
    enableHighAccuracy: true,
    maximumAge: 30000,
    timeout: 10000
});

This is working fine on iOS devices and it even works on the ripple emulator for Android.

But when I try to run the app on Android and more specifically on Nexus 4 and 5, the geolocation doesn't seem to work. It's not that it's giving me an error, it's just trying to retrieve the position without any luck.

However, all the other apps that are using geolocation and also the geolocation in the browser is working perfectly fine!

I would also like to note that I'm using Phonegap Build to build the app.

Does anyone know how I can fix this problem? Did anyone have the same issue?

1

There are 1 best solutions below

0
On

From your code sample, how would you know that there's no error being raised when you have an empty error handler function (error) {}?

getCurrentPosition() makes a single request for the device position. If this is called just once, the position timeout may occur before the GPS hardware on the device has had a chance to get a position fix.

I'd suggest using watchPosition() instead to setup a watcher which will call the success function each time the OS receives a location update. If you only need a single position, you can clear the watcher, optionally checking to make sure you've got a position of acceptable accuracy.

For example:

var minAccuracyInMetres = 50;

var positionWatcher;

function onDeviceReady(){
    positionWatcher = navigator.geolocation.watchPosition(
      geolocationSuccess,
      geolocationError,
      {maximumAge:0, timeout: 10000, enableHighAccuracy: true});
}

function geolocationSuccess(position){
  // Reject if accuracy is not sufficient
  if(position.coords.accuracy > minAccuracyInMetres){
      return;        
  }

  // Only single position required so clear watcher
  navigator.geolocation.clearWatch(positionWatcher);

  // Do something with the user's location...

}

function geolocationError(error){
  console.warn("Error while retrieving current position. " + 
    "Error code: " + error.code + ",Message: " + error.message);
}

document.addEventListener("deviceready", onDeviceReady, false);