为什么对航点的示例代码的修改失败了?

时间:2013-07-01 20:51:18

标签: javascript html google-maps-api-3

我正在学习如何绘制路线。由于here这个优秀的帖子,这已经完成了。我认为这将是一个简单的过程,在这个例子中添加航路点如下:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps API v3 Directions Example</title> 
   <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></head> 
<body style="font-family: Arial; font-size: 12px;"> 
   <div style="width: 600px;">
     <div id="map" style="width: 580px; height: 300px; float: left;"></div> 
   </div>

   <script type="text/javascript"> 

     var directionsService = new google.maps.DirectionsService();
     var directionsDisplay = new google.maps.DirectionsRenderer();

     var orig = new google.maps.LatLng(34.011689,-118.49633);
     var dest = new google.maps.LatLng(34.040818,-118.26815);


     //New coordinate defined below
     var wps1 = new google.maps.LatLng(34.069654,-118.44434);

    //Here is the waypoint 
    var waypts = [{ location: wps1, stopover: true }];

     var map = new google.maps.Map(document.getElementById('map'), {
       zoom:7,
       mapTypeId: google.maps.MapTypeId.ROADMAP
     });

     directionsDisplay.setMap(map);


     var request = {
             origin: orig, 
             destination: dest, 
             waypoints: waypts, //Adding the waypoint here 
             travelMode: google.maps.DirectionsTravelMode.DRIVING
     }



      directionsService.route(request, function(response, status) {
       if (status == google.maps.DirectionsStatus.OK) {
         directionsDisplay.setDirections(response);
       }
     });
   </script> 
</body> 
</html> 

当我测试这个时,我只看到从海滩到洛杉矶市中心的直接路线。我试图让它首先向绕过加州大学洛杉矶分校。

我已经检查了一些Google documentation,但据我所知。我上面做了什么应该工作?我知道,电脑不会撒谎,但不管它是什么都必须是微妙的。

1。我以正确的格式添加了航点。 2。我告诉处理该航点的请求。

出了什么问题?

1 个答案:

答案 0 :(得分:1)

这条航点远离洛杉矶:

 var wps1 = new google.mapsLatLng(38.854782,-94.741055);

它有一个拼写错误(地图和LatLng之间缺少一段时间):

 var wps1 = new google.maps.LatLng(38.854782,-94.741055);

<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
   <title>Google Maps API v3 Directions Example</title>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
</head>
<body>
   <div style="width: 600px;">
     <div id="map" style="width:600px; height:500px;"></div>
   </div>

   <script type="text/javascript">
     var directionsService = new google.maps.DirectionsService();
     var directionsDisplay = new google.maps.DirectionsRenderer();

     var orig = new google.maps.LatLng(34.011689,-118.49633);
     var dest = new google.maps.LatLng(34.040818,-118.26815);


     //New coordinate defined below
     var wps1 = new google.maps.LatLng(34.068921, -118.44518119999998);

    //Here is the waypoint
    var waypts = [{ location: wps1, stopover: true }];

     var map = new google.maps.Map(document.getElementById('map'), {
       zoom:7,
       center: wps1,
       mapTypeId: google.maps.MapTypeId.ROADMAP
     });

     directionsDisplay.setMap(map);


     var request = {
             origin: orig,
             destination: dest,
             waypoints: waypts, //Adding the waypoint here
             travelMode: google.maps.DirectionsTravelMode.DRIVING
     }



      directionsService.route(request, function(response, status) {
       if (status == google.maps.DirectionsStatus.OK) {
         directionsDisplay.setDirections(response);
       } else { alert("Directions Request failed: "+status); }
     });
   </script>
</body>
</html> 

working example