Search code examples
gpsionic-frameworkandroid-4.4-kitkat

Ionic Android navigator.geolocation.getCurrentPosition not worked in Android Kitkat 4.4.4


I build an android Ionic app that using navigator.geolocation.getCurrentPosition, here is my code :

var opsi = {maxAge: 30000, frequency : 30000, enableHighAccuracy: false};

  function success (pos) {
    var lat1 = pos.coords.latitude;
    var long1 = pos.coords.longitude;
    $http.post(link, {
      devicetime : getdtNow(),
      token : token, 
      longlat : {
      lat: lat1,
      long: long1,        
      },
      searchTerm : ''
      }).success(function (res){
          $ionicLoading.hide();
          var s = JSON.stringify(res);
          var reply = JSON.parse(s);
          if (reply.result == 'OK'){
              parkingsites = reply.parkingsites;
              if (maplistview==0){
                gmap($scope,$location);  
                getparkimage($scope);
              }
              else if (maplistview==1){
                getparkimage($scope);
              }
          }
          else if(reply.result == 'TOKENNOTFOUND'){
            state='templates/loginregister.html';
            ct_state = 'loginregister';
            $location.path('/');
            $ionicPopup.alert({
              title: 'App Warning',
              template: 'Sesi Anda Telah Habis'
            });
          }
          else {

          }
      }).error(function (data) {
        $ionicLoading.hide();
        $ionicPopup.alert({
          title: 'App Warning',
          template: 'Koneksi Ke Server Bermasalah'
        });
      });
  };

  function error(err) {
    $ionicLoading.hide();
    $ionicPopup.alert({
      title: 'App Warning',
      template: err.message
    });
    //console.warn('ERROR(' + err.code + '): ' + err.message);
  };

  navigator.geolocation.getCurrentPosition(success, error, opsi);

When I try my app in android devices with all OS except Kitkat 4.4.4, that code is working well, but when I try in Android 4.4.4, it always called function error, with message time out expired, anybody can help ?


Solution

  • I have this working on android 4.4.4 by just using the following code:

    function getUserPosition () {
           var positionOptions = {timeout: 10000, enableHighAccuracy: true};
           return $ionicPlatform.ready()
            .then(function () {
              return $cordovaGeolocation.getCurrentPosition(positionOptions);
            }, function (error) {
              console.error("Error getting user position: " + angular.toJson(error));
            });
         }
    

    I am using the cordova geolocation plugin.

    Cordova version: 5.4.1, ionic framework version: 1.2.1, ionic cli version: 1.7.12.