Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Why cordova geolocation watch doesn't working?

    • 1
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 1.31k
    Answer it

    Hello.I'm making an application in ionic.I'm using cordova geolocation plugin.I want when i change my position location to be updated .I should see an alert but i don't see anything.What i should do ? Here is my code:

    $ionicPlatform
                            .ready(function() {
                                // location detector
    
                                var posOption = {
                                    timeout : 1000,
                                    enableHighAccuracy : false
                                };
                                $cordovaGeolocation.getCurrentPosition(
                                        posOption).then(function(position) {
                                    $scope.lat = position.coords.latitude;
                                    $scope.long = position.coords.longitude;
                                    console.log($scope.lat);
                                    socket.emit('my other event', {
                                        latitude : $scope.lat,
                                        longitude : $scope.long
                                    });
                                }, function(err) {
                                    console.log(err)
                                });
                                var watchOptions = {
                                    frequency : 1000,
                                    timeout : 3000,
                                    enableHighAccuracy : false
                                // may cause errors if true
                                };
                                var watch = $cordovaGeolocation
                                        .watchPosition(watchOptions);
                                watch.then(null, function(err) {
                                    // error
                                }, function(position) {
                                    var lat = position.coords.latitude
                                    var long = position.coords.longitude
                                    $window.alert(lat);
                                    socket.emit('my other event', {
                                        latitude : lat,
                                        longitude : long
                                    });
                                });
                                watch.clearWatch();
    

 1 Answer(s)

  • Hello Surdu ,

    Well going through you code I think you are not using position detection trigger properly it has to be something like :

    navigator.geolocation.watchPosition

    Use it something like this :

    var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 30000 });

    function onSuccess(position) { var element = document.getElementById('geolocation'); element.innerHTML = 'Latitude: ' + position.coords.latitude + '
    ' + 'Longitude: ' + position.coords.longitude + '
    ' + '


    ' + element.innerHTML; }

    // onError Callback receives a PositionError object // function onError(error) { alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n'); }

    If you use the above example properly I think you will be able to get the position on change .

    If the issue still exist please let us know

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: