I am developing an application which 70% of it is based on geolocation. I began to develop in the iOS and everything works great, I had some problems but nothing that is not solvable logically. Now, I was asked a version for Android, at first impression, everything works identical to iOS, but when the testing version that give me problems arose geolocation. The problem is that being off the GPS from the phone (I tested Samsung, Xperia, Motorolas) fall into the error function (which I defined) to alert the user that the GPS was off and that should enable it to occupy. However, activating it, it does not work well, restart the computer to work correctly recently, I’m crazy, because even I get an error response, or alerts you leave, or anything like that.
My code :
$scope.$on('$ionicView.enter', function(){
$(function(){
document.addEventListener("deviceready", onDeviceReady, false);
})
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
function onSuccess(pos) {
$scope.latitud = pos.coords.latitude
$scope.longitud = pos.coords.longitude;
console.log(pos.coords.latitude);
$scope.mostrar_cercanos = true;
$scope.getViews();
}
function onError(error) {
$scope.mostrar_cercanos = false;
$scope.mostrar_mensaje_gps = true;
$scope.mensaje = "El GPS de su telefono no se encuentra activado";
}
})
Hi, getting the same problem on Lollipop. When gps is turned off and user asked to turn on gps, once that is completed, going back and calling the function will not work. Only when app is closed and opened again will it work.
Thanks for your help @Gajotres! I solved it by having a couple of loops, but if the user does not turn on their GPS, when they come back to the app, they have to click on the button again, which is a bit troublesome.
Here is my code to deal with it:
Hi @zachary545. Can u please explain how you solved the GPS turn on issue? When user turned on the GPS and again calling the getCurrentPosition function it is returning error.
There are 3 functions, getLocation, checkLocation and resumeLocation.
The only method that I actually call in my html file is getLocation. What it does is pause then call function checkLocation. What checkLocation does is to check if location has been turned on, and switches to the device settings so that the user can turn it on with ease. Once that is done, when the user goes back to the app, the function getLocation will be called again, which would trigger checkLocation again followed by resumeLocation which then produces the GPS coordinates.
Reminder, it can only work in an actual device and not the browser. Also, there are certain plugins that need to be added (Geolocation, InAppBrowser, Toast & Dianostic).
The plugin exists mainly because the cordova geolocation plugin does not use Android code anymore : https://issues.apache.org/jira/browse/CB-5977. It relies on the geolocation capability of the WebView. Don’t know why no one is talking about this.