我只是试图使用Google Maps API获取地图上2点的距离。使用GDirections。问题是函数完成后,距离总是空。我知道这是因为直到函数完成后才会调用“加载”事件。事件监听器不返回值,所以我很难过!来自Google Maps API中GEvent.addListener的返回值?
有没有人知道如何让这个函数返回距离?也许有更好的方法来获得谷歌地图API中2点之间的距离?
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
}