2017-09-14 138 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以下,允许将总是返回作为理所当然的。