2

I am using angular.js with phonegap. To get users gps location I developed a simple service:

app.factory('geolocation', function ($rootScope, cordovaReady) {
    return {
        getCurrentPosition: cordovaReady(function (onSuccess, onError, options) {

            navigator.geolocation.getCurrentPosition(function () {
                var that = this,
                    args = arguments;

                if (onSuccess) {
                    $rootScope.$apply(function () {
                        onSuccess.apply(that, args);
                    });
                }
            }, function () {
                var that = this,
                    args = arguments;
                if (onError) {
                    $rootScope.$apply(function () {
                        onError.apply(that, args);
                    });
                }
            }, options);
        });
    }
});

I call this service like this

geolocation.getCurrentPosition(function (position) {
   console.log(position);
});

Now I want to extend the service with another method called getCurrentCity. This method should use getCurrentPosition to determine the city. So the service would look like this:

app.factory('geolocation', function ($rootScope, cordovaReady) {
    return {
        getCurrentPosition: ....
        getCurrentCity: ...
    }
});

Is this possible? How should I implement getCurrentCity by using getCurrentPosition?

1 Answer 1

1

Forgive me if I'm answering the wrong question here, but if you're just asking how to return multiple methods from a service, it's quite simple:

angular.module('app', []).factory('geolocation', function ($rootScope, cordovaReady) {
    var locationObj = {
        getCurrentPosition: cordovaReady(function (onSuccess, onError, options) {

            navigator.geolocation.getCurrentPosition(function () {
                var that = this,
                    args = arguments;

                if (onSuccess) {
                    $rootScope.$apply(function () {
                        onSuccess.apply(that, args);
                    });
                }
            }, function () {
                var that = this,
                    args = arguments;
                if (onError) {
                    $rootScope.$apply(function () {
                        onError.apply(that, args);
                    });
                }
            }, options);
        }),

        getCurrentCity: function() {
            this.getCurrentPosition(function(position){
                //determine city
                var city = ...
                //how to apply city to scope?
            });
        }
    }

    return locationObj;
});

If you're asking a phonegap-specific question, please disregard!

Sign up to request clarification or add additional context in comments.

4 Comments

No this is not phonegap specific. I will try your suggestion.
hm I get "getCurrentPosition is not defined" in getCurrentCity method
Try changing it to this.getCurrentPosition()
I edited your answer so that it works for me, in addition how do I apply the city variable to scope so that I can pass it to frontend?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.