0

In my AngularJS project I am using the following code to get a device's GPS co-ordinates:

// 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.

Does any one know how I can amend the code above to trigger an error in this scenario? Any help would be much appreciated.

londonfed
  • 1,170
  • 2
  • 12
  • 27

2 Answers2

0

As pointed out in this post Is there a way to check if geolocation has been DECLINED with Javascript?, you can pass a second callback to getCurrentPosition which will get called if the permission is declined.

Community
  • 1
  • 1
unobf
  • 7,158
  • 1
  • 23
  • 36
0

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 + ").";
        }
    });
};
londonfed
  • 1,170
  • 2
  • 12
  • 27