tags:

views:

19

answers:

1

I am performing a Google local search, and wish to return the lat/long via an object. The Google search itself works fine.

When inspected (using console.log or alert()) in the object itself, the results field appears to be populated OK.

However, when inspecting the instance of the object (instantiated before running the callback) the result is blank. I'm aware I don't need the accessor function - the end result is the same either way.

Is there something fundamental missing here? Thanks!

function GeoSearch() {  

    this.results = [];  

    this.searchComplete = function(localSearch) {  

        if(localSearch.results[0]) {  
            var resultLat = localSearch.results[0].lat;  
            var resultLng = localSearch.results[0].lng;  

            this.results = localSearch.results[0].lat;  
        }

     }

    this.getResults = function() {   
        return this.results;   
    }  

}  

function populateCoords(postcode) {  
     var localSearch = new google.search.LocalSearch();  
     var gs = new GeoSearch();  

     localSearch.setSearchCompleteCallback(gs, gs.searchComplete, [localSearch]);  
     localSearch.execute(postcode + ", UK");  

     alert(gs.getResults());  
}
+1  A: 

When you reference the function gs.searchComplete, you're detaching the method from the object it belongs to. Switch it to function () { gs.searchComplete(); }

Andy E
This works just as well to populate the object (although retrieving the result still fails), but it appears that the real problem is the async aspect of it (something I'm not used to - mainly working in PHP). Thanks.
Andy