navigator.geolocation.getCurrentPosition sometimes not working fine


#1

I am using navigator.geolocation.getCurrentPosition to get current location from user and i have implemented this as
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
console.log(“on device ready end”);
}
// onSuccess Geolocation
function onSuccess(position) {

    console.log("success");
   
    console.log(position);
    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 />';

    console.log(element.innerHTML);

}
but sometimes its not working properly to get lat and long in some devices

can anyone suggest me?


#2

do you gave permission to access gps loacation from the code ?


#3

yes rahulpzy

sometimes it retrieves the lat,long and working fine and sometimes not


#4

do you know about enableHighAccuracy ? are you using it


#5

no rahulpzy
I am not using it in my code


#6

try to use the enableHighAccuracy:false and give some timeout


#7

if you are interested in cordova geolocation plugin i can help you with my code


#8

sure rahulpzy

i will thank you for ur help


#9

step1:just install cordova geolocation plugin…
step2:add $cordovaGeolocation directive in your controller…
var lastUpdateTime = null;
var minFrequency = 560 * 1000;
$scope.watchOptions = { timeout: 6
60* 1000, enableHighAccuracy: false, maxAge: 1000, };

$scope.locWatch = $cordovaGeolocation.watchPosition($scope.watchOptions);

$scope.locWatch.then(null, function (err) {
    //ionicToast.show('Unable to get location', "top", true, 2500);
}, function (position) {
    var now = new Date();
   
    if (lastUpdateTime && now.getTime() - lastUpdateTime.getTime() < minFrequency) {
        console.log("Ignoring position update");
        return;
    }
    UlastUpdateTime = now;
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
 // here am using $scope.watchOptions and adjust the watch option timer according to your need

//if you have any doudt in my code ask me
//my need is to send my device location between 5 minute…
//$scope.watchOptions is some time not working in my android device so im comparing with the time interval