My goal is to retrieve location coordinates using the Google Maps JavaScript API in an asynchronous manner.
Below is the function I've created for this purpose:
function fetchCoordinates(address)
{
var geocoder = new google.maps.Geocoder();
var addr = {
address: address
};
var callback = function(result, status)
{
if (status == "OK") {
var coords = result[0]['geometry']['location'];
console.log(coords.toUrlValue());
}
};
geocoder.geocode(addr, callback);
}
I aim to include these coordinates in a form submission using an AJAX function.
During testing, the following code snippet produced the following output:
form.submit(function(event){
event.preventDefault();
var addressInput = $("input[type='text']").val();
fetchCoordinates(addressInput);
console.log('should wait');
});
This resulted in:
should wait
coordinates
I'm seeking advice on how to ensure that the fetchCoordinates
function completes before moving on to the next instruction.