I’m trying to create an XML formatted output in a textarea but have run into a asynch problems:
$(document).ready(function() {
var geocoder;
geocoder = new google.maps.Geocoder();
$('#xmloutput').val('<?xml version="1.0" encoding="UTF-8"?>\n<parent>\n');
var addresslist = 'one\ntwo\nthree';
var addlines = addresslist.split('\n');
$.each(addlines, function(name, value) {
geocoder.geocode( { 'address': value}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$('#xmloutput').val($('#xmloutput').val()+'<node>'+value+'</node>\n');
}
});
});
$('#xmloutput').val($('#xmloutput').val()+'</parent>');
});
I want this output:
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<node>one</node>
<node>two</node>
<node>three</node>
</parent>
But I get this output because the geocoding takes a while…
<?xml version="1.0" encoding="UTF-8"?>
<parent>
</parent><node>one</node>
<node>two</node>
<node>three</node>
I’ve seen a lot of similar posts and the fix seemes to be chaining or callback, but I’ve not managed to get anything working yet. How should I approach this?
Thanks!
Ben
Change your
eachloop and add the closing tag on last pass of the loopIt is still possible for geocoder to return values out of sequence due to asynchronous nature of call to service. If this happens you may need to create a local object of all results and use a deffered to check all results received prior to loading the complete string