距离矩阵谷歌地图javascript api3

发布于 2024-11-08 17:50:45 字数 1039 浏览 0 评论 0原文

我玩距离矩阵 http://code.google.com/apis/maps/documentation /javascript/services.html#distance_matrix

获取从一个起点到多个目的地的持续时间。 我有这个代码:

var duration = new Array();
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
        origins: [origin],
        destinations: destination,
        travelMode: google.maps.TravelMode.DRIVING,
        avoidHighways: false,
        avoidTolls: false
    }, 
    function(response, status)
    {
        if (status == google.maps.DistanceMatrixStatus.OK)
        {
            var destinations = response.destinationAddresses;
            var results = response.rows[0].elements;

            for (var j = 0; j < results.length; j++)
                duration[j] = results[j].duration.value;
        }
    });
    alert(duration[0]);

但我有警报“未定义”。当我将警报命令放入回调函数中时,我得到了我想要的警报。这是为什么???我该如何修复它?

提前谢谢你!

I play with distance matrix
http://code.google.com/apis/maps/documentation/javascript/services.html#distance_matrix

to get the duration from one origin to multiple destinations.
I have this code:

var duration = new Array();
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
        origins: [origin],
        destinations: destination,
        travelMode: google.maps.TravelMode.DRIVING,
        avoidHighways: false,
        avoidTolls: false
    }, 
    function(response, status)
    {
        if (status == google.maps.DistanceMatrixStatus.OK)
        {
            var destinations = response.destinationAddresses;
            var results = response.rows[0].elements;

            for (var j = 0; j < results.length; j++)
                duration[j] = results[j].duration.value;
        }
    });
    alert(duration[0]);

but i have alert "undefined". when i put the alert command inside the callback function i have the alert i want. why is that??? how can i fix it?

Thank u in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-11-15 17:50:45
var duration = new Array();
function calculate_distance(){
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
        origins: [origin],
        destinations: destination,
        travelMode: google.maps.TravelMode.DRIVING,
        avoidHighways: false,
        avoidTolls: false
    }, 
    function(response, status)
    {
        if (status == google.maps.DistanceMatrixStatus.OK)
        {
            var destinations = response.destinationAddresses;
            var results = response.rows[0].elements;

            for (var j = 0; j < results.length; j++)
                duration[j] = results[j].duration.value;
        }
    });
}
google.maps.event.addListener(autocomplete, 'place_changed', calculate_distance);
alert(duration[0]);

尝试类似的事情

var duration = new Array();
function calculate_distance(){
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
        origins: [origin],
        destinations: destination,
        travelMode: google.maps.TravelMode.DRIVING,
        avoidHighways: false,
        avoidTolls: false
    }, 
    function(response, status)
    {
        if (status == google.maps.DistanceMatrixStatus.OK)
        {
            var destinations = response.destinationAddresses;
            var results = response.rows[0].elements;

            for (var j = 0; j < results.length; j++)
                duration[j] = results[j].duration.value;
        }
    });
}
google.maps.event.addListener(autocomplete, 'place_changed', calculate_distance);
alert(duration[0]);

Try anything like that

伴梦长久 2024-11-15 17:50:45

将警报移至回调中。该函数是异步执行的回调。

Move the alert in to the callback. The function is a callback that is executed asynchronously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文