1

I don't understand why I can not take the value from a function and to display it in another in angular. My code:

$scope.getLatitudeLongitude = function(address) {
            var geocoder = new google.maps.Geocoder();
            $scope.latLng = [];
            geocoder.geocode( { "address": address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
                    var location = results[0].geometry.location,
                        lat      = location.lat(),
                        lng      = location.lng();
                    $scope.latLng.push(lat,lng);
                    return $scope.latLng;
                }

            });
};


$scope.save = function () {

            $scope.address = $scope.getAddress();
            $scope.getLatitudeLongitude($scope.address).then(function(){
                console.log($scope.latLng);
            })
 }

Any ideas?

7
  • $scope.getLatitudeLongitude() should return a promise. Check what it is returning now. It is returning an array. Commented Jul 26, 2017 at 12:12
  • what doesnt work?/ Commented Jul 26, 2017 at 12:13
  • ok. you dont have a promise to use then Commented Jul 26, 2017 at 12:13
  • This code is all in the same controller right? Commented Jul 26, 2017 at 12:14
  • looks like $scope.getLatitudeLongitude($scope.address) is normal function. No need of then to call in your code. Commented Jul 26, 2017 at 12:14

2 Answers 2

1

Adding a promise to getLatitudeLogitude like this should work. Inject $q too in controller

$scope.getLatitudeLongitude = function(address) {
                var deferred = $q.defer();
                var geocoder = new google.maps.Geocoder();
                $scope.latLng = [];
                geocoder.geocode( { "address": address }, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
                        var location = results[0].geometry.location,
                            lat      = location.lat(),
                            lng      = location.lng();
                        $scope.latLng.push(lat,lng);
                       deferred.resolve($scope.latLng);
                    }
                    else{
                         // geocode error
                     deferred.reject();
                  }

                }
               return deferred.promise;

    };


$scope.save = function () {

           $scope.address = $scope.getAddress();
           $scope.getLatitudeLongitude($scope.address)
                .then(function(latLng){
                     console.log(latLng);
                },function(){
                     //log your error here;
                };
};
Sign up to request clarification or add additional context in comments.

5 Comments

it gives me error saying: Cannot read property 'then' of undefined
did u click on the error and find out which line is the error
@IleNea can you check now. the geocode success call back was not in the way I expected it to be
Thank you very much, for look at this bug: if for example I write this: $scope.getLatitudeLongitude($scope.address).then(function(latLng){ console.log($scope.val = latLng[0]); }); console.log($scope.val); -> the $scope.val is undefined... why is happening?
0
    $scope.getLatitudeLongitude = function(address) {
                var geocoder = new google.maps.Geocoder();
                $scope.latLng = [];
                geocoder.geocode( { "address": address }, function(results, status) {
                    if (status === google.maps.GeocoderStatus.OK && results.length > 0) {
                        var location = results[0].geometry.location,
                            lat      = location.lat(),
                            lng      = location.lng();
                        $scope.latLng.push(lat,lng);
                        //return $scope.latLng; you don't need to return when the method execute the $scope will know and keep the value visible to $scope.save
                    }

                });
    };

    $scope.getAddress = function(){
//perform your scope.getAddress process here and after you get your address do this
$scope.addressRecieved = addressReturned;
      };

    $scope.save = function () {

              //  $scope.address = $scope.getAddress();
$scope.address = $scope.addressRecieved;
if($scope.address !== undefined) {               
   $scope.getLatitudeLongitude($scope.address);
}else{
console.log('Address not defined!');
}
         console.log($scope.latLng);           

     }

//trigger the $scope.getAddress first, you can even call the $scope.save inside the $scope.getAddress method, to ensure that the address is always defined else this programme won't work as expected. //You can even use callback e.g function(){$scope.getAddress, callback}

Comments

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.