如何根据百度地图计算出两地之间的驾驶距离

<html>
<head>
    <title>计算驾车时间与距离</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
    body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}
    #l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
    #r-result{height:100%;width:20%;float:left;}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=zMXzBkKIs9weKTo5kCz278MIG8UW9ACP"></script>
    
</head>
<body>
    <div id="allmap"></div>
    <script type="text/javascript">
        var map = new BMap.Map("allmap");
        map.centerAndZoom("郑州",12);
        var output = "需要";
        var searchComplete = function (results){
            if (transit.getStatus() != BMAP_STATUS_SUCCESS){
                return ;
            }
            var plan = results.getPlan(0);
            output += plan.getDuration(true) + "\n";        //获取时间
            output += "总路程为:" ;
            output += plan.getDistance(true) + "\n";       //获取距离
        }
        var transit = new BMap.DrivingRoute(map, {renderOptions: {map: map},
            onSearchComplete: searchComplete,
            onPolylinesSet: function(){    
            setTimeout(function(){alert(output)},"1000");
        }});
        transit.search("市体育中心地铁站", "郑州火车站地铁站");
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/xiyafei122/article/details/89680198