I want to show an alert saying that "Please turn on your GPS".
How can I get GPS status using phonegap?
I have used following code but I don't get any alert message if GPS is turned off. 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>
The answer to your question is below.
You cannot check *if gps is enabled*You can set a timeout and get the timeout. You can do this by setting the
timeout
parameter in the geolocationoptions parameter. (which you did not use)From the documentation
navigator.geolocation.getCurrentPosition
orgeolocation.watchPosition
until the correspondinggeolocationSuccess
callback executes. If thegeolocationSuccess
callback is not invoked within this time, thegeolocationError
callback is passed aPositionError.TIMEOUT
error code. (Note that when used in conjunction withgeolocation.watchPosition
, thegeolocationError
callback could be called on an interval everytimeout
milliseconds!) (Number)WAIT I WAS WRONG
There is now a plugin that checks to see if the GPS is turned on.
In fact, more than one.
Assuming we're talking just about Android platform only, as @Joerg rightly says, you can use cordova.plugins.diagnostic to check if GPS is enabled using
isGpsLocationEnabled()
:If the result is that GPS is switched off, you can either switch the user to the location settings page to manually enable GPS:
Or, in addition, you could use cordova-plugin-request-location-accuracy to request high accuracy location mode (i.e. GPS) directly from within the app. This will show a native confirm dialog and if user agrees, GPS will be enabled automatically:
The best way to check if GPS, Wifi, … is enabled, is to use this plugin:
https://www.npmjs.com/package/cordova.plugins.diagnostic
Using this plugin you can check a lot of features and you can switch the user to the settings.