Domanda

In the below code when I stop the code in FireBug, the "results" object is a nice array filled with good geocoded values.

However, the locations[] array I put it into is full of all "undefined" values.

Shouldn't that be impossible because I can see the results[0] each time working nicely?

 var locations = [];

    function makeCallback(addressIndex) {
        var geocodeCallBack = function (results, status) {
            if (status == "success") {
                locations[addressIndex] = results[0];
            }
        };
        return geocodeCallBack;
    }

    $(function() {
        for (var x = 0; x < addresses.length; x++) {
            $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=' + addresses[x], null, makeCallback(x));
        }
    });
È stato utile?

Soluzione

After line:

var geocodeCallBack = function (results, status) {

results is object with properties results, success, for example Object {results: Array[1], status: "OK"} so the line:

locations[addressIndex] = results[0];

has to be changed to

locations[addressIndex] = results.results[0];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top