2017-09-14 139 views
0

我正在構建一個Android Ionic應用程序,該應用程序每次都使用cordova.geolocation.getCurrentPosition GPS中斷,我無法獲取當前位置,因此它會移至錯誤功能。cordova.getCurrentLocation()方法無法正常工作

在較低版本的Android(5.0.0)中工作正常,但在較高版本(6.0.1及以上版本)中打破。

這裏是我的代碼:

var options = { 
    timeout: 10000, 
    maximumAge: 100, 
    enableHighAccuracy: false 
}; 
$cordovaGeolocation.getCurrentPosition(options).then(function(position) { 
    $scope.UserCurrentLat = position.coords.latitude; 
    $scope.UserCurrentLng = position.coords.longitude; 
    var source = new google.maps.LatLng($scope.UserCurrentLat, $scope.UserCurrentLng); 
    var destination = new google.maps.LatLng(Data.data.geoLocation.coordinates[0], Data.data.geoLocation.coordinates[1]); 
    var directionsService = new google.maps.DirectionsService(); 
    $scope.userRestDistance = parseFloat((google.maps.geometry.spherical.computeDistanceBetween(source, destination)/1000).toFixed(1))+" km"; 
    $ionicLoading.hide(); 
}, function(error) { 
    $ionicLoading.hide(); 
    if (error.PERMISSION_DENIED || error.POSITION_UNAVAILABLE) { 
     $ionicLoading.hide(); 
     $location.path("/noGPS"); 
    } 
}); 
+0

請正確格式化您的代碼。 –

回答

0

的Android 6+需要使用您的位置運行時的權限。通過提出一個請求來獲取您當前的位置,由於拒絕訪問您的位置的權限,它會失敗。

一種選擇是使用cordova-diagnostic-plugin親自管理位置授權:

function runMyCode(){ 
    var options = { 
     timeout: 10000, 
     maximumAge: 100, 
     enableHighAccuracy: false 
    }; 
    $cordovaGeolocation.getCurrentPosition(options).then(function(position) { 
     $scope.UserCurrentLat = position.coords.latitude; 
     $scope.UserCurrentLng = position.coords.longitude; 
     var source = new google.maps.LatLng($scope.UserCurrentLat, $scope.UserCurrentLng); 
     var destination = new google.maps.LatLng(Data.data.geoLocation.coordinates[0], Data.data.geoLocation.coordinates[1]); 
     var directionsService = new google.maps.DirectionsService(); 
     $scope.userRestDistance = parseFloat((google.maps.geometry.spherical.computeDistanceBetween(source, destination)/1000).toFixed(1))+" km"; 
     $ionicLoading.hide(); 
    }, function(error) { 
     $ionicLoading.hide(); 
     if (error.PERMISSION_DENIED || error.POSITION_UNAVAILABLE) { 
      $ionicLoading.hide(); 
      $location.path("/noGPS"); 
     } 
    }); 
} 

function handlePermissionResponse(status){ 
    switch(status){ 
     case cordova.plugins.diagnostic.permissionStatus.GRANTED: 
      runMyCode(); 
     break; 
     case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED: 
      requestLocationPermssion(); 
      break; 
     case cordova.plugins.diagnostic.permissionStatus.DENIED: 
      requestLocationPermssion(); 
      break; 
     case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS: 
      console.error("Permission permanently denied - notify user"); 
      break; 
    } 
} 

requestLocationPermssion(){ 
    cordova.plugins.diagnostic.requestLocationAuthorization(handlePermissionResponse, function(error){ 
     console.error(error); 
    }); 
} 

function checkLocationPermission(){ 
    cordova.plugins.diagnostic.getLocationAuthorizationStatus(handlePermissionResponse, function(error){ 
     console.error(error); 
    }); 
} 

// check/request permission first 
checkLocationPermission(); 

注:在Android上5以下,允許將總是返回作爲理所當然的。