app.controller('dashboard', function($scope){
$scope.getPosition = function(position){
$scope.acc = position.coords;
$scope.lat = position.coords.latitude;
$scope.lng = position.coords.longitude;
$scope.$apply();
};
$scope.getPositionErr = function(error){
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
};
navigator.geolocation.getCurrentPosition($scope.getPosition, $scope.getPositionErr, {maximumAge: 0, timeout: 6000, enableHighAccuracy:false});
});
I am using Angular JS and Cordova for an Android app. This code is working fine on desktop but not on Nexus 4 with Lollipop. Also controller code is executing after deviceready as per requirement from Cordova
I have tried
But I am unable to get lat, long and geolocation always gets timeout.
It only works when I enabled Location/GPS from setting. After enabling it code is working as expected.
navigator.geolocation.getCurrentPosition is only for GPS when option "enableHighAccuracy" is set as 'true'(default). That's why you are not able to get current location via mobile device. About "it is working on desktop", I believe you must have opened GPS setting in your browser.
Usually I used code below t o get position no matter GPS is open or not.
var positionOption = { timeout: 500, enableHighAccuracy: true };
var gpsSunccuss = function(currentPosition) {
//use gps position
};
var gpsFailed = function() {
//use some 3rd party position solution(get position by your device ip)
getPositionBy3rdParty();
};
navigator.geolocation.getCurrentPosition(gpsSunccuss, gpsFailed, positionOption);
On the other hand, you could set enableHighAccuracy as 'false'.
I had identical issue and this fixed it for me
{enableHighAccuracy:false,maximumAge:Infinity, timeout:60000}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With