Geocoding in a loop - need help with closures and callbacks

You must Login before you can answer or comment on any questions.

I am trying to geocode addresses in a database. I've run into this before and can't remember how to create the callback in such a way it gets the right data. Right now the callback routine runs, but always gets the last variable in the array. I think I need to pass in a function or something, but don't understand closures well enough with javascript to fix it myself. Anyone help?

var counternow = 1;
 
function callbackFunction( callback_Passed_Contact )
                    {
                        Ti.API.info('I got the contact record looks like this ' + JSON.stringify( callback_Passed_Contact));
                        updatedb( passed_Contact.longitude, passed_Contact.latitude, passed_Contact.contactID);  //update the SQL database with long,lat
                        //Ti.API.info('Proessing ' + counternow + ' of ' + dbCount);
                        //Ti.API.info('updating db ' + passed_Contact.contactID + ' with ' + passed_Contact.longitude + ' '+ passed_Contact.latitude);
                        ++counternow;
                    };                  
 
 
 
function geocodeDBAddresses()
{   
            var contactToProcess = new Object();
 
 
 
            var geocode = function( callback, passed_Contact )
            {
                    var url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + passed_Contact.address + "&sensor=true";
 
                    Ti.API.info('I got the contact  inside geocode looks like this ' + JSON.stringify( passed_Contact )  );
 
                                        var xhr = Ti.Network.createHTTPClient(
                                        {
                                                onload: function(e)
                                                   {
                                                        var json = JSON.parse( this.responseText);
                                                        //Ti.API.info( json );
                                                        passed_Contact.longitude = json.results[0].geometry.location.lng;
                                                        passed_Contact.latitude = json.results[0].geometry.location.lat;
 
                                                        Ti.API.info('Passing to callback  this ' + JSON.stringify( passed_Contact ));
 
                                                        callback( passed_Contact );  //pass the contact to the callback function for processing
                                                    },
 
                                                onerror: function(e) {
                                                    // this function is called when an error occurs, including a timeout
                                                    alert('error looking up ' + passed_Contact.contactName);
                                                },
 
                                                timeout:5000  /* in milliseconds */
                                        });  //end createHTTPClient
 
                                        sleep(100);                 
                                        xhr.open("GET", url);
                                        xhr.send(); 
 
            }// end geocode
 
            var db = Ti.Database.open('iMap.db');
            var rows = db.execute('SELECT * FROM CONTACTSTABLE');
            var dbCount = rows.getRowCount();
 
 
 
            while ( rows.isValidRow() )
                        {
                            contactToProcess.contactID = rows.fieldByName('contactID');
                            contactToProcess.contactName = rows.fieldByName('contactName');
                            contactToProcess.addressType = rows.fieldByName('addressType');
                            contactToProcess.address = rows.fieldByName('address');
                            contactToProcess.longitude = rows.fieldByName('longitude');
                            contactToProcess.latitude = rows.fieldByName('latitude');
                            contactToProcess.image = rows.fieldByName('image');
 
                            //geocode( callbackFunction, contactToProcess);
 
                            rows.next();
                        }
 
            rows.close();  //close the recordSet
 
 
 
            db.close();  // closes the database
 
            mapview.opacity = 1;
 
};//end function geocodeaddresses

Your Answer

Think you can help? Login to answer this question!