访问单个Google地图元素(距离,时间,路线)

时间:2009-10-14 23:37:22

标签: google-maps

如何访问Google地图中的各个元素?

有没有办法通过API执行此操作,还是我必须解析DOM并分别拉出元素(例如距离,时间,方向)? API的第2版或第3版没问题。

示例:

<div jstcache="6" style="text-align: right; padding-bottom: 0.3em;"
jseval="this.innerHTML = $Route.summaryHtml">38.9&nbsp;mi (about 40 mins)</div>

我想要Javascript计算的距离(例如,38.9英里)。如果API中不存在任何内容,我将手动解析它。

谢谢, 标记

注意:这是我正在使用的完整示例网站:http://code.google.com/intl/en-EN/apis/maps/documentation/examples/directions-advanced.html

使用简化解决方案进行更新

对于那些需要它的人来说,这是一个非常简单的完整HTML页面,我可以从Cannonade发布的示例中细化出来。这删除了所有样式和其他脚本:

<html>
    <head>
    <script src="maps.google.com/maps?file=api&amp;v=2.x&a…; type="text/javascript"></script>
    <script>
    function initialize() 
    {
        alert("loading ..."); 
        if (GBrowserIsCompatible()) 
        { 
            var wp = new Array (); 
            wp[0] = new GLatLng(32.742149,119.337218); 
            wp[1] = new GLatLng(32.735347,119.328485); 
            directions = new GDirections(); directions.loadFromWaypoints(wp);
            GEvent.addListener(directions, "load", function() 
            { 
                alert(directions.getDuration().seconds); 
            }); 
        } 
        else 
        { 
            alert("Sorry, the Google Maps API is not compatible with this browser"); 
        } 
    }

    </script>  
    </head>  
    <body onload="initialize();" onunload="GUnload();"></body> 
</html>

将代码示例放入index.html,您将被设置。

1 个答案:

答案 0 :(得分:0)

您可以在回复中获取GDirections :: loadFromWaypoints请求的各个属性。

  • getDuration - 旅行时间的持续时间(秒)。
  • getDistance - 以米为单位的距离或描述距离的本地化字符串。

您可以找到获取旅行时间heresrc)的示例。

相关问题