How do I pass callback results through an object?

I'm doing a local google search and want to return lat / long via an object. Google search itself works great.

When checked (using console.log or alert ()) in the object itself, the result field will be filled with OK.

However, when inspecting an instance of an object (created when the callback runs), the result is empty. I know I don't need an accessor function - the end result is the same anyway.

Is there something fundamentally missing here? Thank!

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());  
}

      

+2


a source to share


1 answer


When you refer to a function gs.searchComplete

, you override the method from the object it belongs to. Switch it tofunction () { gs.searchComplete(); }



+1


a source







All Articles