谷歌地图驾驶路线示例的源代码?

19

你打算使用V2 API还是V3?后者更为推荐,因为V2已经被弃用。 - Daniel Vassallo
如果建议使用V3版本,我正在学习,所以我想要一些源代码。 - user310291
请提供获取源代码以便理解的链接! - Ashish Dwivedi
2个回答

73

这是一个非常基本的示例,使用v3 API

<!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"></script>
    </head> 
    <body style="font-family: Arial; font-size: 12px;"> 
       <div style="width: 600px;">
         <div id="map" style="width: 280px; height: 400px; float: left;"></div> 
         <div id="panel" style="width: 300px; float: right;"></div> 
       </div>
       
       <script type="text/javascript"> 
    
         var directionsService = new google.maps.DirectionsService();
         var directionsDisplay = new google.maps.DirectionsRenderer();
    
         var map = new google.maps.Map(document.getElementById('map'), {
           zoom:7,
           mapTypeId: google.maps.MapTypeId.ROADMAP
         });
        
         directionsDisplay.setMap(map);
         directionsDisplay.setPanel(document.getElementById('panel'));
    
         var request = {
           origin: 'Chicago', 
           destination: 'New York',
           travelMode: google.maps.DirectionsTravelMode.DRIVING
         };
    
         directionsService.route(request, function(response, status) {
           if (status == google.maps.DirectionsStatus.OK) {
             directionsDisplay.setDirections(response);
           }
         });
       </script> 
    </body> 
    </html>

截图:

Google Maps API v3 Directions Example


2
谢谢,这正是我想要的:一个极简的学习示例。 - user310291
嗨,丹尼尔,我已经实现了你的答案,它很有效,我稍微进行了修改,我在请求中添加了** alternative:true **,所以它给了我多条路线,如果我点击其中一条路线,它会立即在地图上显示路线,现在我想知道用户点击了哪条路线?有没有办法保存该路线的详细信息?你能给我提些建议吗? - Deepu
@Fraser 感谢你的回答,Fraser。实际上这是一个很久以前的问题,但感谢你在这里留下的评论。非常感激。 - hardik
1
@hardik 没问题。我看到这是一段时间以前的事情,但如果有其他人来到这篇帖子需要帮助,我想提供帮助。谢谢。 - Fraser
@Daniel Vassallo:我想在iOS移动应用程序中展示您在附加的屏幕截图中展示的基于文本的方向指示。请问您能提供我获取该响应的API吗? - Sudheer Kumar Palchuri
显示剩余7条评论

3

使用JavaScript通过纬度/经度或地址获取路线、路径和其他数据的代码,不使用 Google 地图,使用 v3 API

<script src="https://maps.googleapis.com/maps/api/js?v=3&key=<key>&libraries=places"></script>

<script>
    var directionsService = new google.maps.DirectionsService();

    var start_lat = "41.8781136";
    var start_lon = "-87.6297982";
    var end_lat = "40.7127753";
    var end_lon = "-74.0059728";

    // Using Latitude and longitude
    var request = {
        origin: new google.maps.LatLng(start_lat, start_lon),
        destination: new google.maps.LatLng(end_lat, end_lon),
        optimizeWaypoints: true,
        avoidHighways: false,
        avoidTolls: false,
        travelMode: google.maps.TravelMode.DRIVING
    };

    //Using plain address
    // var request = {
    //     origin: { query: "Chicago, IL, USA" },
    //     destination: {  query: "New York, NY, USA" },
    //     travelMode: google.maps.TravelMode.DRIVING,
    // };

    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            var route = response.routes[0];
            var leg = response.routes[0].legs[0];
            var polyline = route.overview_polyline;
            var distance = route.legs[0].distance.value;
            var duration = route.legs[0].duration.value;

            console.log(route); // Complete route
            console.log(distance); // Only distance 
            console.log(duration); // Only duration
            console.log(leg); // Route options/list
            console.log(polyline); // Polyline data
        }
    });

</script>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接