Reputation: 102
I program an app where I need GPS. If the GPS is off, how can I ask the user to turn on GPS, like google maps? After a while of googleing I found nothing compareable... My other question is, why I get "Use location? - 2", when the GPS is off? I thought, if the GPS is off, $window.navigator.geolocation is false?
controller.js
if ($window.navigator && $window.navigator.geolocation) {
function success(pos) {
$rootScope.position = { x: pos.coords.latitude, y: pos.coords.longitude };
}
function fail(error) {
alert("Use location? - 2");
}
$window.navigator.geolocation.getCurrentPosition(success, fail, { maximumAge: 500000, enableHighAccuracy: true, timeout: 6000 });
} else {
alert("Use location? - 1");
}
Upvotes: 2
Views: 1292
Reputation: 3
The cordova-plugin-request-location-accuracy
cordova plugin does trigger the native dialog window to ask the user to enable GPS from the app without having to navigate to the phone's settings and then back to you app.
https://github.com/dpa99c/cordova-plugin-request-location-accuracy
Only down side is that it only works on Android. It's not possible to turn on GPS from your app on iOS devices with this plugin. BUT, as mentioned in the plugin's documentation, you can use the cordova.plugins.diagnostic
plugin to navigate to the phone's settings and manually turn on the GPS as a fallback for iOS devices.
I have done a lot of research on that and at the moment, this is the best solution I have found so far. I know the the Google Maps app on iOS does it, so it should be possible from a Cordova plugin, but I guess no one has done it yet.
Upvotes: 0
Reputation: 488
There are many plugins that promess to help you get if your gps is enable. Then you can ask the user to change Location Settings. One of them is the Diagnostics Plugin https://www.npmjs.com/package/cordova.plugins.diagnostic . I am using it. (al least to call the Location Settings).
Upvotes: 2