Phonegap: how to check if gps is turned on

I want to show a warning that says "Please turn on the GPS."

How can I get GPS status using a phone saver?

I used the following code, but I don’t get a warning message if GPS is disabled. Instead, nothing happens.

<!DOCTYPE html> <html> <head> <title>Device Properties Example</title> <script type="text/javascript" charset="utf-8" src="cordova.js"></script> <script type="text/javascript" charset="utf-8"> // Wait for device API libraries to load // document.addEventListener("deviceready", onDeviceReady, false); // device APIs are available // function onDeviceReady() { var test= navigator.geolocation.getCurrentPosition(onSuccess, onError); } // onSuccess Geolocation // function onSuccess(position) { var element = document.getElementById('geolocation'); element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' + 'Longitude: ' + position.coords.longitude + '<br />' + 'Altitude: ' + position.coords.altitude + '<br />' + 'Accuracy: ' + position.coords.accuracy + '<br />' + 'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' + 'Heading: ' + position.coords.heading + '<br />' + 'Speed: ' + position.coords.speed + '<br />' + 'Timestamp: ' + position.timestamp + '<br />'; } // onError Callback receives a PositionError object // function onError(error) { alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n'); } </script> </head> <body> <p id="geolocation"> Finding geolocation...</p> </body> </html> 
+6
source share
3 answers

Assuming this is only an Android platform, as @Joerg says correctly, you can use cordova.plugins.diagnostic to check if GPS is isGpsLocationEnabled() using isGpsLocationEnabled() :

 cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){ console.log("GPS location is " + (enabled ? "enabled" : "disabled")); }, function(error){ console.error("The following error occurred: "+error); }); 

If GPS is disabled as a result, you can switch the user to the location settings page to manually enable GPS:

 cordova.plugins.diagnostic.switchToLocationSettings(); 

Or, in addition, you can use cordova-plugin-request-location-accuracy to request high-precision mode (e.g. GPS) directly from the application. This will display a confirmation dialog, and if the user agrees, GPS will be turned on automatically:

 function onRequestSuccess(success){ console.log("Successfully requested accuracy: "+success.message); } function onRequestFailure(error){ console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message); if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){ if(window.confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){ cordova.plugins.diagnostic.switchToLocationSettings(); } } } cordova.plugins.locationAccuracy.request(onRequestSuccess, onRequestFailure, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY); 
+17
source

The best way to check if GPS, Wifi, ... is on - use this plugin:

https://www.npmjs.com/package/cordova.plugins.diagnostic

Using this plugin, you can test many functions, and you can switch the user to settings.

+2
source

The answer to your question is below .

You cannot check * if gps * is enabled

You can set a timeout and get a timeout. You can do this by setting the timeout parameter to the geolocationoptions parameter. (which you did not use)

From the documentation

  • timeout:. The maximum length of time (milliseconds) that is allowed to go from a call to navigator.geolocation.getCurrentPosition or geolocation.watchPosition until the corresponding geolocationSuccess callback is geolocationSuccess . If the geolocationSuccess not called during this time, the geolocationError is passed . ( , geolocation.watchPosition geolocationError timeout !) () . ( , geolocation.watchPosition geolocationError timeout !) ()

WAIT, I WRONG

Now there is a plugin that checks if GPS is turned on.

In fact, more than one.

+1
source

All Articles