When I start the App with GPS off , and later GPS

2019-07-25 11:35发布

I am having problems with GPS detection over Google Maps in Ionic App. When I start the App with GPS off and later I change from Settings device to GPS on, clicking by Find Me button - centerOnMe() function - the GPS is not working. The function responses always

`Error: GPS Timeout! ERROR = 3` 

At the beginning I thought that was a cache problem but I have found out that is not that. It doesn´t matter the using $state.reload from the view. ¿does anyone get the same issue? Thanks for the support. I attach the function below:

  $scope.centerOnMe = function() {

        $ionicPlatform.ready(function() {
        console.log('Click on Find Me');

        if(!map) {
            return;
        }

        $ionicLoading.show({
            content: 'Getting current location...',
            showBackdrop: false
        });

        //$state.reload('app.map');
        watchPositionId = navigator.geolocation.watchPosition(function(position) {                    
                navigator.geolocation.clearWatch(watchPositionId);
                var latitude     = position.coords.latitude;
                var longitude    = position.coords.longitude;
                $scope.latitude  = latitude;
                $scope.longitude = longitude;
                var geolocpoint  = new google.maps.LatLng(latitude, longitude);
                map.setCenter(geolocpoint);     
        });
        navigator.geolocation.getCurrentPosition(function (position) {
                   var lat  = position.coords.latitude
                   var long = position.coords.longitude                
                   $scope.center = new google.maps.LatLng(lat, long);
                   $ionicLoading.hide();
                   console.log("GPS is enabled" + $scope.center);
         }, function(err) {
                  // error
                  if (err.code == 1)     { console.log('GPS disabled! ERROR = '       + err.code);  }
                  else if(err.code == 2) { console.log('GPS is unavailable! ERROR = ' + err.code);  }
                  else if(err.code == 3) { console.log('Error: GPS Timeout! ERROR = ' + err.code);  }   
                  $ionicLoading.hide();
         }, {
                  enableHighAccuracy: true, timeout: 20000
            }  
         );

       });      
    };

1条回答
Luminary・发光体
2楼-- · 2019-07-25 11:59

You better to use this plugin,

 cordova plugin add cordova.plugins.diagnostic

It will directly check whether gps is enabled or not.

Fore more details check following ionic forum issues,

  1. Geo location timeout vs location permission error
  2. Get Current Position not working after turning the GPS off on the device even if it turned on again?

Hopes this will help you !!

查看更多
登录 后发表回答