【问题标题】:Google maps get directions谷歌地图获取路线
【发布时间】:2012-12-08 02:27:34
【问题描述】:

我正在使用这段代码来获取两个位置之间的方向,但这段代码只是显示地图,并没有绘制任何方向。

$('#map_canvas').gmap({ 
    'center': new google.maps.LatLng(32.498467,74.542126) 
});
$('#map_canvas').gmap('loadDirections', 'panel', { 
    'origin': new google.maps.LatLng(32.498467,74.542126), 
    'destination': new google.maps.LatLng(29.070574,76.112457), 
    'travelMode': google.maps.DirectionsTravelMode.DRIVING 
});

等待帮助。

提前致谢。

【问题讨论】:

    标签: jquery jquery-ui google-maps jquery-mobile google-maps-api-3


    【解决方案1】:

    这是我经常使用的代码:
    它显示地图上的路线和#info div中的方向信息

    <form id="route">                      
       <label>Address: <input type="text" id="addr" /></label>
       <button id="route">Calculate route</button>
    </form>
    <div id="map_canvas"></div>
    <div id="info"></div>
    
    $(function(){
       var center = new google.maps.LatLng(32.498467,74.542126);
       var map = new google.maps.Map(document.getElementById("map_canvas"),{
          center: center,
          zoom: 14,
          mapTypeId: google.maps.MapTypeId.ROADMAP
       });
       var m = new google.maps.Marker({map:map,position:center});
       var dD = new google.maps.DirectionsRenderer({map:map,panel:$('#info')[0]});
    
       $('#route').submit(function(ev){
          var addr = $('#addr').val();
          var ds = new google.maps.DirectionsService();
          ds.route({
             origin:addr,
             destination:center,
             region:'at',
             travelMode: google.maps.TravelMode.DRIVING
          },function(result,status){
          // if(status == google.maps.DirectionsStatus.OK){
                dD.setDirections(result);  
                $('#info').fadeIn(200);             
          // }
          });
          ev.preventDefault();
       });
    });
    

    【讨论】:

    • 您能否修改它,使其接受两个 lat,lng 值并显示结果。
    • @h_a86 我刚刚在我们正在使用它的页面上对其进行了测试,它也适用于 lat lng 值。 (例如:32.498467,74.542126)。所以只需将其设置为origin。它可能也适用于 google.LatLng 对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2012-03-28
    • 2015-10-19
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    相关资源
    最近更新 更多