how to get callback response while use $http.post method inside of GPS detector function using factory in angularjs

I try to throw error modal window when the gps not enable. perhaps it's enabled then it will call servcie callback using $http.post method. But without gps function it's working and return promises fine. if use GPS method above the post method then i got some error like "Uncaught TypeError: Cannot read property 'then' of undefined" What's wrong with my code. Anyone can give me some idea..

Service Factory:

   .service('DataServiceFactory', function($http, $rootScope){         //dynamic $http JSON Data Passing service
            return{
                serviceDataReq : function(reqUrl, reqData){
                    cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
                        if(enabled == 1){
                            return $http.post(reqUrl, reqData);
                        }else{
                            $rootScope.ErrorMsg = "Please Turn On your GPS location..";
                            $('#ErrorModal').modal('show');
                        }
                    }, function(error){
                        $rootScope.ErrorMsg = "GPS location Error : "+error;
                        $('#ErrorModal').modal('show');
                    })
                }
            }
        })

In controller:

DataServiceFactory.serviceDataReq($rootScope.primaryLiveServiceApiUrl, angular.toJson($scope.LoginAuthReq)).then(function(response) {

}, function(error){

});


ANSWERS:


I don't have a cordova example right now to test your code, but looking at it, I would say that if the device don't have gps capabilities, your service method is not returning a promise.

I would do something like this:

.service('DataServiceFactory', function($q, $http, $rootScope){         //dynamic $http JSON Data Passing service
         return {
            serviceDataReq: function(reqUrl, reqData){
                var deferred = $q.defer();
                cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
                    if(enabled == 1){
                      deferred.resolve($http.post(reqUrl, reqData));
                    }else{
                      $rootScope.loaded = false;
                      $rootScope.ErrorMsg = "Please Turn On your GPS location..";
                      $('#ErrorModal').modal('show');
                      deferred.reject("Please Turn On your GPS location..");
                    }
                }, function(error){
                    $rootScope.ErrorMsg = "GPS location Error : "+error;
                    $('#ErrorModal').modal('show');
                    deferred.reject("GPS location Error : "+error);
                });
              return deferred.promise;
            }
          };
    })


 MORE:


 ? Error while calling AngularJS service function in controller?
 ? Error while calling AngularJS service function in controller?
 ? Error while calling AngularJS service function in controller?
 ? AngularJS service delay API call
 ? How to call angular service method from controller?
 ? AngularJs Service function- this()
 ? How to use a service from directive to call a function?
 ? Pass $http service in AngularJS to normal javascript function
 ? How to pass data between different modules using service having two different controller with different modules?
 ? How to pass data between different modules using service having two different controller with different modules?