When I start the App with GPS off , and later GPS is activated, Location is not detected

谁说我不能喝 提交于 2020-01-05 07:14:06

问题


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:


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 !!



来源:https://stackoverflow.com/questions/38277660/when-i-start-the-app-with-gps-off-and-later-gps-is-activated-location-is-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!