queue

by Igor Cuckovic

HTML

<script src="http://d3js.org/queue.v1.min.js"></script>

JavaScript

var data = [
            {customer: {"city": "Osijek","country": "Croatia"}, support: {"city": "Zagreb","country": "Croatia"}},
            {customer: {"city": "Rijeka","country": "Croatia"}, support: {"city": "Zagreb","country": "Croatia"}},
            {customer: {"city": "Split","country": "Croatia"}, support: {"city": "Zagreb","country": "Croatia"}},
            {customer: {"city": "Nasice","country": "Croatia"}, support: {"city": "Zagreb","country": "Croatia"}},
            {customer: {"city": "Pula","country": "Croatia"}, support: {"city": "Zagreb","country": "Croatia"}}
           ];



var path = "http://maps.googleapis.com/maps/api/geocode/json?address=";

//var locations = ["osijek", "zagreb","split", "rijeka"];

var together = [];
data.forEach(function(d){ together.push([path + d.customer.city, path + d.support.city]); });
//console.log(together)
//var tasks = [path, path1, path2, path3];

/*
d3.json(path, function(error, json) {
  if (error) return console.warn(error);
  data = json;
  console.log(data.results[0].geometry.location)
});
*/
/*
queue()
    .defer(d3.json, path)
    .defer(d3.json, path1)
    .await(function(error, data, data1) { console.log(data.results[0].geometry.location, data1.results[0].geometry.location); });
*/

var customerLoc = [];
var supportLoc = [];

var q = queue(1);
together.forEach(function(t) { q.defer(d3.json, t[0]); 
                               q.defer(d3.json, t[1]);});

q.awaitAll(function(error, results) { 
    console.log(results); 
    
    for (var i = 0; i < results.length; i += 1) {
        if (i % 2 !== 0) {
            customerLoc.push({"customer": results[i-1].results[0].formatted_address, "support": results[i].results[0].formatted_address});
    }
     }
    
    /*
    results.forEach(function (d, i) {
        //customerLoc.push(d.results[0].geometry.location)
        // {customer: {"lat": 40.25,"lng":-80.70}, support: {"lat": 45.30,"lng":-120.79}}
        if (i % 2 === 0) {
           ...