Skip to content Skip to sidebar Skip to footer

Phonegap Geolocator Using Wifi Instead Of Gps

I am trying to implement geolocation on a PhoneGap App running on Android. For some reason the locator only returns accurate distances when connected WiFi (i.e. it prefers coarse o

Solution 1:

Ensure you have have geolocation enabled in your config.xml: <plugin name="Geolocation" value="org.apache.cordova.GeoBroker"/>

And in your AndroidManifest.xml:

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

Also try upping your timeout to 60000 - it can take a while for older devices to acquire a GPS fix.

Consider using navigator.geolocation.watchPosition instead of navigator.geolocation.getCurrentPosition. This will add a watcher which calls the success function each time the device acquires a new position; the error callback will be called if a position fix hasn't yet been acquired.

One more thing (I found from experience) is that Android will silently fall back to Wifi or cell triangulation if it loses its GPS fix and there's nothing in the Position object passed to the success function to tell you what hardware the device is using to obtain its location. However, you can infer it from the accuracy and discard any positions that are too inaccurate, something like:

MIN_ACCURACY = 20; //metresfunctionsuccess(position){
  if(position.coords.accuracy > MIN_ACCURACY){
    console.log("Position rejected because accuracy is less than required "+MIN_ACCURACY+" metres");
    return;
  }
  // else, do some stuff with the position
}

You might also want to consider using this plugin to check if GPS is enabled on the device with isGpsEnabled() and if not, direct the user to the settings page with switchToLocationSettings()

Hope it helps...

Post a Comment for "Phonegap Geolocator Using Wifi Instead Of Gps"