简体   繁体   English

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

[英]Trigger JavaScript error when geo location services disabled

In my AngularJS project I am using the following code to get a device's GPS co-ordinates: 在我的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;;
    });
};

The problem is when location services is turned off on an iPhone 6, there is a no error created to inform the user that they need to turn on location services. 问题是当在iPhone 6上关闭位置服务时,不会创建任何错误来通知用户他们需要打开位置服务。

Does any one know how I can amend the code above to trigger an error in this scenario? 在这种情况下,有人知道如何修改上面的代码以触发错误吗? Any help would be much appreciated. 任何帮助将非常感激。

As pointed out in this post Is there a way to check if geolocation has been DECLINED with Javascript? 如本文所指出的, 有没有一种方法可以检查Javascript是否拒绝了地理位置定位? , you can pass a second callback to getCurrentPosition which will get called if the permission is declined. ,您可以将第二个回调传递给getCurrentPosition,如果权限被拒绝,则将调用该回调。

Thanks for pointing me in the right direction unobf. 感谢您为我指出正确的方向。 Please find attached the code (with updated error handling) in case any one stumbles across this. 如果发现有人偶然发现此代码,请查找随附的代码(具有更新的错误处理)。

 // 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 + ").";
        }
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM