2016-03-15 77 views
0

我正在使用MapQuest单张Api ​​绘制多个站点(自定义标记)的路线。一切都快完成了。我得到一个路线多个标记和一个多重线。MapQuest Leaflet Api - 获取时间和距离的优化路径

我有两个问题

  1. 如何绘制优化路由的onClick的
    按钮路由优化
    代码是这样的
dir = MQ.routing.directions(); 
      dir.optimizedRoute({ 
       locations: [ 
        '33.703507, 73.053702', 
         '33.714328, 73.050625', 
         '33.730497, 73.077898', 
         '33.732863, 73.088078' 
       ] 
      }); 
  • 如何获得泰坦总路线和时间开车?下面

    <script> 
        window.onload = function() { 
           var map, 
            dir; 
           var custom_icon, 
            marker; 
           map = L.map('map', { 
            layers: MQ.mapLayer(), 
            center: [40.045049, -105.961737], 
            zoom: 7 
           }); 
    
           dir = MQ.routing.directions(); 
           dir.route({ 
            locations: [ 
             '33.703507, 73.053702', 
             '33.714328, 73.050625', 
             '33.730497, 73.077898', 
             '33.732863, 73.088078' 
            ], 
            options: { avoids: ['toll road'] } 
           }); 
        CustomRouteLayer = MQ.Routing.RouteLayer.extend({ 
             createStopMarker: function (location, stopNumber) { 
    
    
              custom_icon = L.divIcon({ 
               iconSize: [26, 36], 
               popupAnchor: [0, -18], 
               html: '<span class="notification">' + stopNumber + '</span>' 
              }); 
              marker = L.marker(location.latLng, { icon: custom_icon }).bindPopup(location.adminArea5 + ' ' + location.adminArea3).openPopup().addTo(map); 
    
              marker.on('click', onMarkerClick); 
    
              return marker; 
             } 
            }); 
        map.addLayer(new CustomRouteLayer({ 
            directions: dir, 
            fitBounds: true, 
            draggable: false, 
            ribbonOptions: { 
             draggable: false, 
             ribbonDisplay: { color: '#CC0000', opacity: 0.3 }, 
             widths: [15, 15, 15, 15, 14, 13, 12, 12, 12, 11, 11, 11, 11, 12, 13, 14, 15] 
            } 
           })); 
        } 
        </script> 
        <body style='border:0; margin: 0'> 
         <div id='map' style='position: absolute; top: 0; bottom: 0; width: 100%;'></div> 
        </body> 
    

  • 我的代码,请给予帮助。谢谢:)

    回答

    1

    该按钮将调用一个函数,其中包含MQ.routing.directions.optimizedRoute()代码。

    function optimize() { 
        dir = MQ.routing.directions(); 
    
        dir.optimizedRoute({ 
        locations: [ 
         'syracuse ny', 
         'springfield ma', 
         'ithaca ny', 
         'hartford ct' 
        ] 
        }); 
    
        map.addLayer(MQ.routing.routeLayer({ 
        directions: dir, 
        fitBounds: true 
        })); 
    } 
    

    要获得里程,请使用成功事件来获得整个路线响应。

    dir = MQ.routing.directions() 
        .on('success', function(data) { 
        console.log(data.route.distance); 
        }); 
    
    +0

    非常感谢。它工作完美。 data.route.distance将以米或公里为单位的距离? data.route.time会在几分钟或几秒内给出时间? –

    +0

    为什么这段代码运行两次? dir = MQ.routing.directions() .on('success',function(data){ console.log(data.route.distance); }第一次给出确切的值,下一次给出未定义的值。 ); @MQBrian –

    +0

    它第二次运行来自routeshape请求。在进一步处理之前,您可以检查响应中的对象。默认距离以英里为单位,但单位参数可以在选项中更改。时间以秒为单位。 – MQBrian