In my visits collection I have a geocodeVisit function which uses the Google geocoding service to gecode an address. The problem is that the meteor script is typically run before the google maps API is loaded, resulting in an Exception while invoking method 'visitInsert' ReferenceError: google is not defined error. So I need to wait with the inser till the geocoding has finished. How can I do this? This is the visits collection:
Meteor.methods({
visitInsert: function(visitAttributes) {
check(Meteor.userId(), String);
check(visitAttributes, {
nr: String,
visit_date: String
});
var properties = {
userId: Meteor.userId(),
position: geocodeVisit(visitAttributes.address)
};
var visit = _.extend(visitAttributes, properties);
var visitId = Visits.insert(visit);
return {
_id: visitId
};
}
});
geocodeVisit = function (address) {
this.unblock;
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location;
}
});
}