【问题标题】:Why is this modification of sample code for waypoints failing?为什么航点示例代码的这种修改失败?
【发布时间】:2013-07-01 20:51:18
【问题描述】:

我正在学习如何绘制路线。这是因为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。我告诉请求处理那个航路点。

出了什么问题?

【问题讨论】:

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


    【解决方案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

    【讨论】:

    • 好的,你应该得到一些支持,这是回答。但作为临别评论,这是自然的后续问题。是否有一种“简单”的方法来绘制任意数量的航点?我很生气 google api 说我们只能使用 8 个航点。如果我有 100 个航点,因为我要去阿拉斯加或类似的地方旅行怎么办?我想我可以制作多条路线,但具有讽刺意味的是,在谷歌上搜索如何实现这一点并没有给出一个简单的答案。我猜他们设计它是故意弄坏的?
    猜你喜欢
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    相关资源
    最近更新 更多