我只是試圖使用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
}