0

Simply i need call an API to get response of user addresses and corresponding to each address i have to call google API to get the longitude and latitude.
I have tried too much and about all methods but at last my i am still looking to my empty hands.
please help me figure out where i am wrong.

app.controller('myController', function($scope, $rootScope, $http,$q){

$scope.myGeoKey="AIzaSyCqoOf_Kx5wzrYh3ioxFQ_xyz";

// FIRST API REQUEST
var url="http://xyz/abc?origin=noida";
$http.get(url, {
        headers:{'Content-Type': 'application/json'}
}).
then(function(data, status, headers, config) {

    // response is like
    /*
       [
            {
                "address":"Sector 64, Noida",
                "state": "Uttar Pradesh"
            },
            {
                "address":"Sector 62, Noida",
                "state": "Uttar Pradesh"
            },
            {
                "address":"CP, New Delhi",
                "state": "Delhi"
            }
        ]
    */

    var synchronizeData = [];
    var asynchronizeData = [];

    for (var key in data.data.response){

        var rawAddress=data.data.response[key].address+", "+data.data.response[key].state;
        var geoUrl="?address="+rawAddress+"&key="+$scope.myGeoKey;

        // GET LATITUDE AND LONGITUDE FOR EACH API RESPONSE

        $http.get("https://maps.googleapis.com/maps/api/geocode/json"+geoUrl).
        then(function(data, status, headers, config) {

            if(data.data.status=="OK") {
                var geometry=data.data.results[0].geometry.location;

                var latlng={
                    "lat": geometry.lat,
                    "long": geometry.lng
                }
                asynchronizeData.push(latlng);
            }

        });
    }

    // console.log(asynchronizeData) // WORKING TILL HERE

    $q.all(asynchronizeData).then(function(response){

        // console.log(response) // THIS IS NOT WORKING

        for (var i=0,len = response.length;i<len;++i){
            synchronizeData.push(response[i]);
        }
        $scope.synchronized = synchronizeData;

        console.log($scope.synchronized); // FINAL OUTPUT NOT WORKING

    });
});
});

Please Please..
Thanks

1 Answer 1

0

.all() need array of promises as param (or hash of promises). And you pass an array of literal objects!

Read the doc about $q

I am really not sure about that but, try to push all your second http call (the looped one) in your asyncArray. Then tell us what is happen in your .all() function.

Because each $http Will return a promise, so you stack promises in your array, so you can .all().

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

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.