禁用地理位置服务时触发JavaScript错误

时间:2014-12-27 12:05:17

标签: javascript angularjs html5

在我的AngularJS项目中,我使用以下代码来获取设备的GPS坐标:

// when user clicks on geo button
$scope.getGeoLocation = function() {
    var geocoder = new google.maps.Geocoder();
    window.navigator.geolocation.getCurrentPosition(function(position) {
        $scope.$apply(function() {
            $scope.position = position;
            var latlng = new google.maps.LatLng($scope.position.coords.latitude, $scope.position.coords.longitude);
            geocoder.geocode({'latLng': latlng}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                $scope.searchstring = results[2].formatted_address;
                $location.search('s', $scope.searchstring);
                $location.search('p', 1);
                $location.search('geo', true);
                $route.reload();
              }
            });
        });
    }, function(error) {
        $scope.error = error;;
    });
};

问题是在iPhone 6上关闭位置服务时,没有创建错误来通知用户他们需要打开位置服务。

有没有人知道如何修改上面的代码以在这种情况下触发错误?任何帮助将非常感激。

2 个答案:

答案 0 :(得分:0)

正如本文Is there a way to check if geolocation has been DECLINED with Javascript?所指出的那样,您可以将第二个回调传递给getCurrentPosition,如果权限被拒绝,将会调用该回调。

答案 1 :(得分:0)

感谢你指出我正确的方向unobf。如果任何人遇到此问题,请查找随附的代码(更新的错误处理)。

 // when user clicks on geo button
$scope.getGeoLocation = function() {
    var geocoder = new google.maps.Geocoder();
    window.navigator.geolocation.getCurrentPosition(function(position) {
        $scope.$apply(function() {
            $scope.position = position;
            var latlng = new google.maps.LatLng($scope.position.coords.latitude, $scope.position.coords.longitude);
            geocoder.geocode({'latLng': latlng}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                $scope.searchstring = results[2].formatted_address;
                $location.search('s', $scope.searchstring);
                $location.search('p', 1);
                $location.search('geo', true);
                $route.reload();
              }
            });
        });
    }, function(error) {
        $scope.error = "";   
        // Check for known errors
        switch (error.code) {
            case error.PERMISSION_DENIED:
                $scope.error = "This website does not have permission to use " + 
                          "the Geolocation API.";
                alert("Geo location services appears to be disabled on your device.");
                break;
            case error.POSITION_UNAVAILABLE:
                $scope.error = "The current position could not be determined.";
                break;
            case error.PERMISSION_DENIED_TIMEOUT:
                $scope.error = "The current position could not be determined " + 
                          "within the specified timeout period.";            
                break;
        }
        // If it's an unknown error, build a $scope.error that includes 
        // information that helps identify the situation, so that 
        // the error handler can be updated.
        if ($scope.error == "")
        {
            var strErrorCode = error.code.toString();
            $scope.error = "The position could not be determined due to " + 
                      "an unknown error (Code: " + strErrorCode + ").";
        }
    });
};