Using .getJSON to Update Array Not Working

Using .getJSON to Update Array Not Working

Hi all,

I have the following code which I am using with Google Maps:


  1. var allLocations = new Array();


  2. function getResults(){
       
        // create the boundary for the data
        var bounds = map.getBounds();
       
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
       
       
        var getVars = "&ne=" + northEast.toUrlValue() + "&sw=" + southWest.toUrlValue() + "&z=" + map.getZoom();

        // run query to return results from db
        $.getJSON("mapService.php?a=u" + getVars, function(resultData) {
           
           
           
            //Test Output
                var html = new StringBuilder();
      
       html.append("<strong>resultData:</strong> <br />");
      
       html.append("resultData length: " + resultData.Locations.length + "  <br /><br />");
           
            allLocations = [];
            allLocations = resultData.Locations;
           
            html.append("allLocations length inside resultData: " + allLocations.length + "<br /><br />");
           
            html.append("allLocations Contents:<br /> " + allLocations.toSource());
           
            $("#infopane_content").html(html.toString());
           
           
        });

        var html = new StringBuilder();
      
       html.append("<strong>allLocations:</strong> <br /><br />");
      
       html.append("allLocations length outside resultData: " + allLocations.length + "  <br /><br />");
      
       html.append("allLocations Contents:<br /> " + allLocations.toSource());
      
      $("#test").html(html.toString());
     
      //End test output

    }















































Problem I am having is that the allLocations array always seems to be one query behind the resultData array. They should always be the same when this is corrected.

For example, the output on page load results in this:

resultData:
resultData length: 5

allLocations length inside resultData: 5


allLocations:

allLocations length outside resultData: 0

allLocations Contents:
[]


Another example when zooming in on the map:

resultData:
resultData length: 1

allLocations length inside resultData: 1


allLocations:

allLocations length outside resultData: 3




In short, the resultData is always updating and displaying the correct array and data but the allLocations array outside of of resultData is incorrect and seems one query behind.