cordova.getCurrentLocation()方法无法正常工作

时间:2017-09-14 05:45:25

标签: cordova ionic-framework

我正在构建一个每次都使用cordova.geolocation.getCurrentPosition GPS中断的Android Ionic应用程序,我无法获取当前位置,因此它会转移到错误功能。

它在较低版本的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");
    }
});

1 个答案:

答案 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及更低版本中,权限将始终返回为GRANTED。