簡體   English   中英

谷歌地圖繪制兩點之間的路線

[英]google maps plot route between two points

我寫了這個無辜的javascript代碼,它允許用戶創建兩個標記並繪制它們之間的路徑。 它不起作用,相反,它給出了一個奇怪的錯誤:

Uncaught TypeError: Cannot read property 'ya' of undefined

有人可以告訴我這里有什么問題:

// called upon a click
GEvent.addListener(map, "click", function(overlay,point) {
    if (isCreateHeadPoint) {
        // add the head marker
        headMarker = new GMarker(point,{icon:redIcon,title:'Head'});
        map.addOverlay(headMarker);
        isCreateHeadPoint = false;
    } else {
        // add the tail marker
        tailMarker = new GMarker(point,{icon:greenIcon,title:'Tail'});
        map.addOverlay(tailMarker);
        isCreateHeadPoint = true;
        // create a path from head to tail
        direction.load("from:" + headMarker.getPoint().lat()+ ", " + headMarker.getPoint().lng()+ " to:" + tailMarker.getPoint().lat() + "," + tailMarker.getPoint().lng(), { getPolyline: true, getSteps: true }); 
        // display the path
        map.addOverlay(direction.getPolyline());
    }
});

根據您的解決方案 ,您可能根本不需要使用map.addOverlay(direction.getPolyline()) 在以下示例中,折線會自動添加到地圖中:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps GDirections</title> 
   <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false" 
           type="text/javascript"></script> 
</head> 
<body onunload="GUnload()"> 

   <div id="map" style="width: 400px; height: 300px"></div> 

   <script type="text/javascript"> 

   var map = new GMap2(document.getElementById("map"));
   var directions = new GDirections(map);
   var isCreateHeadPoint = true;
   var headMarker, tailMarker;

   map.setCenter(new GLatLng(51.50, -0.12), 12);

   GEvent.addListener(map, "click", function(overlay,point) {
      if (isCreateHeadPoint) {
         // add the head marker
         headMarker = new GMarker(point);
         map.addOverlay(headMarker);
         isCreateHeadPoint = false;
      } 
      else {
         // add the tail marker
         tailMarker = new GMarker(point);
         map.addOverlay(tailMarker);
         isCreateHeadPoint = true;
         // create a path from head to tail
         directions.load("from:" + headMarker.getPoint().lat()+ ", " + 
                         headMarker.getPoint().lng() + 
                         " to:" + tailMarker.getPoint().lat() + "," + 
                         tailMarker.getPoint().lng(), 
                         { getPolyline: true, getSteps: true }); 
      }
   });
   </script> 
</body> 
</html>

截圖:

Google Maps GDirections

哦,我得到了......出於一些奇怪的原因,我認為direction.load()是一個阻塞調用。 以下作品:

// called upon a click
GEvent.addListener(map, "click", function(overlay,point) {
    if (isCreateHeadPoint) {
        // add the head marker
        headMarker = new GMarker(point,{icon:redIcon,title:'Head'});
        map.addOverlay(headMarker);
        isCreateHeadPoint = false;
    } else {
        // add the tail marker
        tailMarker = new GMarker(point,{icon:greenIcon,title:'Tail'});
        map.addOverlay(tailMarker);
        isCreateHeadPoint = true;
        // create a path from head to tail
        direction.load("from:" + headMarker.getPoint().lat()+ ", " + headMarker.getPoint().lng()+ " to:" + tailMarker.getPoint().lat() + "," + tailMarker.getPoint().lng(), { getPolyline: true, getSteps: true }); 
    }
});

// called when the direction.load() returns
GEvent.addListener(direction,"load", function() {
    // display the path
    map.addOverlay(direction.getPolyline());
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM