I am trying to create an angular service which uses google map geocoder, however I found the callback from google was executed after the execution of service function.
controller
app.controller( 'AppCtrl',['geocoder',function AppCtrl (geocoder) {
var result=geocoder.geocode('greek');
console.log(result);
});
service
app.service('geocoder',function() {
this.geocode=function(address) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
return results;
} else {
return ('Geocode was not successful for the following reason: ' + status);
}
});
};
});
The problem is , the console.log(result) in the controller outputs undefined and the console.log(results) in the service get the results back from google after the call of the function. That is, the callback function didn't return the results in time.
Is there a way to overcome this problem? Thanks.