I am trying to create a function to return a lat/long array from the Google Maps JS API (v3). I am able to write the data out that I retrieve from the API, but I am not able to pass the data along to a variable. I am wanting to do something like this:
var latLong = getLatLong(Address);
The function that I have created is here:
function getLatLong(loc, callback) {
var geocoder = new google.maps.Geocoder();
var array = new Array();
if (geocoder) {
geocoder.geocode({ 'address': loc }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLng = results[0].geometry.location;;
$.each(latLng, function(k, v) {
if (k == 'va' || k == 'wa') {
array.push(v);
}
});
var ret = array;
callback(ret);
} else {
console.log("Geocoding failed: " + status);
}
});
}
}
I then am assigning the function to a variable and calling it to give me the lat/long.
var geo = getLatLong('Dallas, TX', function(result) {
return result;
});
Unfortunately this produces an 'undefined' result. I can access the result in the function, I just can't pass it along. How can I pass the result of the function to a variable to be accessed?