I'm trying to get location coordinates via the google maps javascript api, which happens asynchronously.
Here is my function for retrieving:
function getCoords(input_address)
{
var geocoder = new google.maps.Geocoder();
var addr = {
address: input_address
};
var callback = function(result, status)
{
if (status == "OK") {
var coords = result[0]['geometry']['location'];
console.log(coords.toUrlValue());
}
};
geocoder.geocode(addr,callback);
}
I want to submit the coordinates along with the rest of a form via an ajax function.
However, testing out the following:
form.submit(function(event){
event.preventDefault();
var addr = $("input[type='text']").val();
getCoords(addr);
console.log('should wait');
});
Outputs:
should wait
coordinates
Is there a way to make sure the getCoords
function completes before the next instruction is executed?