I'm just trying to get the distance from 2 points on the map using the Google Maps API. Using GDirections. The problem is that after the function finishes, the distance is always zero. I know this happens because the load event is not called until the function completes. An event listener does not return a value, so I'm at a standstill!
Does anyone know how I can make this function return a distance? Perhaps there is a better way to get the distance between two points in the 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;
source share