theplau
theplau

Reputation: 980

How do I force minimum accuracy for PhoneGap GPS Geolocation?

I am building an app that requires somewhat accurate GPS position of the user. Right now I'm using:

navigator.geolocation.getCurrentPosition(geolocationSuccess,geolocationError, 
    { maximumAge: 60000, timeout:8000 , enableHighAccuracy: true } );

When running the app however, I'm getting position.coords.accuracy with a value of 29000 sometimes or higher. This is not acceptable. How can I force a lower (more accurate) GPS position?

It's happening on both iOS and Android.

Note I'm using

<gap:plugin name="cordova-plugin-geolocation" source="npm" />

Upvotes: 2

Views: 1017

Answers (1)

Blauharley
Blauharley

Reputation: 4246

The best accurancy can be achieved:

  • The lower maximumAge is set.
  • timeout depends on how your app is used (is it a "navi-app" mostly used in cars or is it "jogger-app" mostly used on foot?). Basic rule: the higher timeout is set the more time you grant your hardware-gps-chip to request accurate coordinates.
  • Of course enableHighAccuracy must be set true as well.

Let me explain the points above in (a little bit) more detail.

When you set maximumAge to 0 (Zero) you never get "live"-gps-coordinates (not cached) back, because even most accurate coordinates have got a certain age. It really depends on your app-requirements which brings me to timeout.

I would set timeout to 5 secs because most modern devices I have used can get coordinates between 0.5-3 secs. It depens on how quick your app is moved (car, on foot). The quicker the app is moved the less timeout has to be set otherwise the app can get too inaccurate coordinates. On the other hand timeout must not be set to 0, because the request would be canceled right now every time and you never get "live"-gps-coordinates back.


You can also make your own plugin or fork a project on github for instance to have more control over:

  • horizontal/vertical accuracies
  • to make sure that the gps-hardware-chip is always used (initialize always Android's LocationManager.GPS_PROVIDER). Because even when you set enableHighAccuracy to true there is no guarantee that the gps-hardware-chip is really used, it's only a "wish" that you tell this plugin to use (if possible) gps-satellites and strongly depends on environmental conditions which brings me to the next paragraph.
  • ....

Note that: It is not always possible to request most accurate coordiantes because of bad environmental conditions (street canyon, strong cloudiness etc.). Is is a requirement that your app has to get coordinates everywhere and everytime? Then you have to loosen your accuracy-requirement for sure.

And last but not least, after a successful request you can always check for the coordinate‘s accuracy-attribute that must not exceed a certain threshold (depending on app-requirement) and if it does then make another request like in this example:

var threshold = "APP-ACCURANCY-REQUIREMENT-NUMBER-HERE";

function requestLocation(callback) {
  navigator.geolocation.getCurrentPosition(
    function (position) {
      console.log(position);
      // Does this position fulfill the ACCURANCY-REQUIREMENT of your app? IF SO THEN STOP REQUESTING OTHERWISE REQUEST AGAIN
      if (position.coords.accuracy < threshold) callback(position);
      else requestLocation(callback);
    }, 
    function (err) {
      console.log(err);
    }, 
    {
      maximumAge: 60000,// IS A 1-MINUTE-COORDINATE TOO OLD WHILE DRIVING BY CAR? 130KM/hour -> 36 M/sec
      timeout: 8000 ,// IS A WAITING-TIME OF 8 SECONDS TOO LONG WHILE DRIVING BY CAR? 
      enableHighAccuracy: true // YES, THIS ATTRIBUTE MUST BE SET TO TRUE
    }
  );
}

requestLocation(function (position) {
  // position that has got an accuracy less than ACCURANCY-REQUIREMENT
  console.log('request accurate position', position);
});

Hope this "story" helps you and Good luck!

Upvotes: 4

Related Questions