2009-10-14 75 views
1

如何访问Google地图中的个别元素?访问个人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> 

将codesamples放入index.html中,您将被设置。

回答

0

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

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

你可以找到一个获取旅行时间的例子heresrc)。

+0

非常感谢。正是我在找什么。感谢您花时间发布解决方案。 对于那些需要它的人来说,这是一个非常简单的完整的HTML页面,我可以从Cannonade发布的例子中精简出来。这具有所有的造型和其他脚本除去: – Mark 2009-10-15 06:41:26

+0

\t \t \t \t \t \t \t <体的onload = “初始化();” onunload =“GUnload();”> \t – Mark 2009-10-15 06:43:04