3

i create one factory method which always returns undefined when retrieving value from controller. when i write log it values come perfectly but returns undefined.

.factory('GetLatLongFromAddress', ['$http', '$q', function ($http, $q) {

  var LatnLong =
      {
          Latitude: 0.00,
          Longitude: 0.00
      }

  return         {
          GetLatLong: function (address) {
              var geocoder = new google.maps.Geocoder();
              var address = address;
              geocoder.geocode({ 'address': address }, function (results, status) {
                  if (status == google.maps.GeocoderStatus.OK)
                  {
                      var latitude = results[0].geometry.location.lat();
                      var longitude = results[0].geometry.location.lng();
                      LatnLong.Latitude = latitude;
                      LatnLong.Longitude = longitude;
                      console.log(LatnLong);
                  }
              });
              setTimeout(function () { }, 3000);
              return LatnLong
          },

      }

}])

And myController in which i am calling is ;

$scope.returnValue=(JSON.stringify(GetLatLongFromAddress.GetLatLong("Address")));        

So, can you help me in this.

Thank You.

2
  • did you inject it? Commented Jul 22, 2016 at 8:36
  • can you help me to give me more detail because i am new in angular Commented Jul 22, 2016 at 8:38

2 Answers 2

3

You are working with asynchronous request to the Google API. So response from API won't be received immediately. In you example you sent request to the API and return LatnLong before response will be received. You have tried to wait for response with setTimeout, but setTimeout function isn't working like that. There is your code sample with comments:

var geocoder = new google.maps.Geocoder();
var address = address;
geocoder.geocode({ 'address': address }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK)
    {
        // This code will be executed when we receive response from Google API.
        // It can be in 2, 3 or 6 seconds. 
        // We can't tell for sure how much time it will take.

        var latitude = results[0].geometry.location.lat();
        var longitude = results[0].geometry.location.lng();
        LatnLong.Latitude = latitude;
        LatnLong.Longitude = longitude;
        console.log(LatnLong);
    }
});
setTimeout(function () { 
    // This code will be executed in 3 seconds.
}, 3000);
return LatnLong // This code will be executed immediately. So LatnLong is undefined.

When you work with asynchronous requests you need to use promises You can find more information by the next link: https://docs.angularjs.org/api/ng/service/$q

In you case next code should work:

var deferred = $q.defer();
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK)
    {
        var latitude = results[0].geometry.location.lat();
        var longitude = results[0].geometry.location.lng();
        var LatnLong = {
            Latitude: latitude,
            Longitude: longitude
        };
        deferred.resolve(LatnLong);
    }
});
return deferred.promise;

This code will return promise and then you can put data into $scope this way:

GetLatLongFromAddress.GetLatLong("Address").then(function(LatLong){
    $scope.returnValue= JSON.stringify(LatLong);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Did you tried without the $timeout ? BTW why an empty timeout here ?

7 Comments

yes. i also tried without timeout. i thought i just make sleep for just 2 seconds in which return variable get data
inject mean add 'GetLatLongFromAddress' to your controller : Ctrl.$inject = ['GetLatLongFromAddress']; function Ctrl(GetLatLongFromAddress) {}
thank you. i added into my controller defination.i.e .controller('addcompanyController', ['$scope', '$http', '$theme', '$location', '$rootScope', '$routeParams', 'GetLatLongFromAddress', function ($scope, $http, $theme, $location, $rootScope, $routeParams, GetLatLongFromAddress) {
and without JSON.stringify ?
without JSON.stringify gives me default value object with 0. i.e { Latitude: 0.00, Longitude: 0.00 }
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.