Timeout at getCurrentPosition in Cordova app on Android

I made a simple Cordova application that wraps the web site of a client. The app asked for the current location using the “official” Geolocation plugin (cordova-plugin-geolocation), but it turned out quite unstable on Android. Sometimes it just stopped providing location. Someone suggested rebooting the phone as a fix, which did help, but is not a very good solution in the long term.

Somewhere I saw that Google suggests using its Play Services API for getting the location, instead of the vanilla Android API. Then I found Google Location Services for Cordova plugin that uses the Play Services location API. Somebody in Stack Overflow suggested that too. The original geolocation plugin gets its location from the webview, which I suppose gets it from the vanilla Android API.

Because the Google Location Services plugin is naturally for Android only, I still use the geolocation plugin on iOS. Here’s the code that uses the Google Location Services plugin if it exists and the default one otherwise:

var locationService = navigator.geolocation;
if (cordova && 'plugins' in cordova && 'locationServices' in cordova.plugins) {
  locationService = cordova.plugins.locationServices.geolocation;
  console.log('Using cordova-plugin-locationservices');
}

locationService.getCurrentPosition(
  function (position) {
    var positionStr = position.coords.latitude + ',' + position.coords.longitude;
    console.log('Got position: ' + positionStr);
    // ...
  },
  function (err) {
    console.log('Did not got position: code=' + err.code + ' message=' + err.message);
    // ...
  },
  {
    enableHighAccuracy: false,
    maximumAge: 30*60*1000, // 30 min
    timeout: 20000 // ms
   }
);