0

I'm trying to inject one geoDataFactory into venueDataFactory. When currentPosition is logged to the console, the result is undefined whereas assuming successful acquisition of coordinates, currentPosition should be an object containing geolocation data including latitude and longitude. Why is it undefined?

angular.module('myApp', ['ionic','ngCordova'])
    .factory('geoDataFactory', function($cordovaGeolocation, $location) {
       var currentPosition = {
           'latitude': 0,
           'longitude': 0,
           'altitude': 0,
           'accuracy': 0,
           'altitudeAccuracy': 0,
           'heading': 0,
           'speed': 0,
           'timestamp': 0
       };

       var getCurrentPosition = function() {
           $cordovaGeolocation.getCurrentPosition().then(function(position) {
               // Position here: position.coords.latitude, position.coords.longitude
               currentPosition.latitude = position.coords.latitude;
               currentPosition.longitude = position.coords.longitude;
               currentPosition.altitude = position.coords.altitude;
               currentPosition.accuracy = position.coords.accuracy;
               currentPosition.altitudeAccuracy = position.coords.altitudeAccuracy;
               currentPosition.heading = position.coords.heading;
               currentPosition.speed = position.coords.speed;
               currentPosition.timestamp = position.coords.timestamp;
               return currentPosition;
           }, 
           function(err) {
               // error
               console.log("Error with cordova geolocation.currentPosition");
               return err;
           });
       };

       return {
           position: getCurrentPosition()
       }
    })
    .factory('venueDataFactory', function($q, $timeout, geoDataFactory) {
        var currentPosition = geoDataFactory.position;
        console.log(currentPosition);
    })
1
  • getCurrentPosition() doesn't return anything, so position is undefined. Should probably return a promise. Commented Aug 14, 2014 at 23:28

1 Answer 1

1

I think position is being returned before getCurrentPosition has returned any data. As Anthony said, you should probably be returning a promise from getCurrentPosition. You can do that by adding return in front of $cordovaGeolocation.getCurrentPosition().then.... That way, position: getCurrentPosition() returns the promise.

Then in your venueDataFactory, you call that function, and when the promise is resolved, it will return the position and you can assign it to your variable. It would look like this:

.factory('venueDataFactory', function($q, $timeout, geoDataFactory) {
    geoDataFactory.position().then(function (response) {
        var currentPosition = response;
        console.log(currentPosition);
    });
})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.