2009-10-14 35 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