I am trying to return a value from an ajax call. I have the following, which is declared in $(document).ready:
var currentUserZip = $("#user").data('zip');
var latLon = [];
I need to take the user's zip code and return coordinates from the google api. I do that using a jquery $.get call below.
function getLatLon(geoCodeQuery, latLon) {
$.get(geoCodeQuery, function(data) {
if (data.results.length > 0) {
latLon.push(data.results[0].geometry.location.lat);
latLon.push(data.results[0].geometry.location.lng);
}
});
}
I call the function and it will fill an array with coordinates based on a user's zip code.
getLatLon(geoCodeQuery, latLon);
However, if I check the value of latLon after the function is called, it returns an empty set.
ie. latLon = []
I would expect the coordinates to exist. Why are the coordinates not retuning? Since this is all done in the $(document).ready portion of the script, shouldn't the value carry through?
Thanks