I am simply trying to get the distance from 2 points on a map using the Google Maps API. Using GDirections. The problem is that after the function finishes, distance is always null. I know this is because the "load" event isn't being called until after the function finishes. The Event Listener doesn't return values either so I am stumped!
Does anybody know how I can make this function return the distance? Perhaps there is a better way to get the distance between 2 points in Google Maps API?
function getDistance(fromAddr, toAddr) {
var distance;
var directions;
directions = new GDirections(null, null);
directions.load("from: " + fromAddr + " to: " + toAddr);
GEvent.addListener(directions, "load", function() {
distance = directions.getDistance().html;
distance = distance.replace(/&.*/, '');
});
return distance; //outputs null
}