I am trying to Map google maps results to cache. The problem is on data variable is always undefined on a first search. It will only have results after the second search (with the same term). Anyone can check whats the problem?
if (!(me.data = me.googleSearchCache[ location ])) {
var geocoderRequest = {
address: location
}
geocoder.geocode(geocoderRequest, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
me.googleSearchCache[ location ] = $.map(results, function(loc) {
return {
value : loc.formatted_address,
//bounds : loc.geometry.bounds,
lat : loc.geometry.location.lat(),
lng : loc.geometry.location.lng()
};
});
}
});
}
data = me.googleSearchCache[ location ];
debug(data);
The google maps geocode request is asynchronous, so the results aren’t available when you are setting your data variable. It will only be immediately available inside the callback of the geocode request. To see, do a console.log inside the callback.