谷歌地图绘制两点之间的路线

时间:2010-05-17 11:55:01

标签: javascript google-maps

我写了这个无辜的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());
    }
});

2 个答案:

答案 0 :(得分:7)

关注from your solution,您可能根本不需要使用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

答案 1 :(得分:0)

哦,我得到了......出于一些奇怪的原因,我认为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());
});