0

I am writing an application that needs the user's current location coordinates. I use the Cordova Geolocation API and am developing in an Intel XDK environment.

In some cellphones (e.g. Samsung S3 and S4), the API cannot obtain the location and instead returns a timeout error. However, sometimes it works after restarting the phone.

I need a stable solution because the location is critical for my project. Here's my current code:

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

I tried calling getCurrentPosition with different parameters (changed maximumAge, timeout, etc.), but nothing seems to work.

Is there anything I can do to fix this issue? Are there perhaps some other plugins that allow me to get the user's location? Alternatively, can I somehow use the cellphones' native features for obtaining the location?

cbr
  • 12,563
  • 3
  • 38
  • 63
halkoy
  • 1
  • Is the GPS turned on in the phones that refuse to work? – cbr Mar 20 '15 at 12:32
  • Did you change the security settings of your device. Go to Settings -> Security -> Check the "Unknown sources" checkbox in order to allow your apk to be run without any problem. – shamaleyte Mar 20 '15 at 13:00
  • @shamaleyte You can't even install programs from outside sources if that's unchecked. – cbr Mar 20 '15 at 13:33
  • @GrawCube you are right, you cannot. But there is a possibility that the "Unknown sources" checkbox has been unchecked after you installed the app. In that case, your app will open but not work as expected. – shamaleyte Mar 20 '15 at 13:56
  • thanks for your replies. But I need a clear solution, I can not say to everyone who installed my application that you should check your phone security settings or to restart their phones. Native android programs does not ask that but they can use location services effeciently like foursquare etc... – halkoy Mar 22 '15 at 14:49

2 Answers2

0

Open the Android Project manifiest and add these permissions

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Then recompile and launch your project.

epergo
  • 565
  • 5
  • 17
  • I have these permissions at my application Have your ever tried cordova gps application with Samsung S3 or S4? Does it work? – halkoy Mar 22 '15 at 14:52
0

getCurrentPosition() makes a single request for the device position. If this is called immediately after the app starts, the timeout (7000ms/7 seconds isn't very long - I'd up it to 30000) may occur before the GPS hardware 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 once you've got a position of acceptable accuracy.

For example:

var minAccuracyInMetres = 50;

var positionWatcher = navigator.geolocation.watchPosition(
  geolocationSuccess,
  geolocationError,
  {maximumAge:0, timeout: 30000, 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...

}
DaveAlden
  • 30,083
  • 11
  • 93
  • 155