First, I'm grabbing some zip codes from a json file. After that is done loading, I loop through the zip codes and connect to
http://api.geonames.org/postalCodeSearchJSON?postalcode="+zip+"&maxRows=1&username=demo
and replace "zip" with the zip code foreach zip code. So if I have 30 zip codes, it calls this URL 30 times and grabs the Latitude and Longitude for each zip code and places them in an array.
The problem is, I'm trying to use my array before it's being fully built by the ajax call. A work around I'm doing is doing a setTimeout() for 3 seconds so the browser has time to fully build the array and then I can use it.
I'm currently using
$.getJSON( "zip-codes.json", function(){}).done(function(locations){
// this .done() isnt actually working
});
Below is all my code:
var marker_object = new Array;
// get location zip codes
$.getJSON( "zip-codes.json", function(){}).done(function(locations){
// for each zip code
$(locations.nodes).each(function(i, location_value){
var name = location_value.node.city+', '+location_value.node.state+'\n'+location_value.node.phone;
var zip = location_value.node.zip;
// if zip is not empty
if(zip != ''){
// get lat, long for each zip
$.getJSON( "http://api.geonames.org/postalCodeSearchJSON?postalcode="+zip+"&maxRows=1&username=midnetmedia", function(){}).done(function(long_lat){
// loop thru each zip code, even tho we only return 1 value at a time
$(long_lat.postalCodes).each(function(k,long_lat_value){
marker_object.push({latLng: [long_lat_value.lat, long_lat_value.lng], name: name});
});
}); // get lat, long for each zip
} // if not empty
}) // foreach location
}); // get zip codes
setTimeout(function(){
console.log( marker_object );
}, 3000);
The end result for the "marker_object" looks something like this when it's finally built
marker_object = [
{latLng: [34.079351, -117.455114], name: 'City 1'},
{latLng: [24.079351, -67.455114], name: 'City 2'},
{latLng: [14.079351, -36.455114], name: 'City 3'},
...
]
How can I tell when my $.getJSON is actually done? because .done() isnt working for me. Should I be using $.AJAX() instead?