JS高德地圖計算兩地之間的實際距離

這個是經過導航的方式來獲取兩地之間的實際距離,和消耗的時間(key值本身去申請哈)javascript

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>位置經緯度 + 駕車規劃路線</title>
    <style type="text/css">
    html,
    body,
    #container {
      width: 100%;
      height: 100%;
    }
    </style>
    <style type="text/css">
        #panel {
            position: fixed;
            background-color: white;
            max-height: 90%;
            overflow-y: auto;
            top: 10px;
            right: 10px;
            width: 280px;
        }
        #panel .amap-call {
            background-color: #009cf9;
            border-top-left-radius: 4px;
               border-top-right-radius: 4px;
        }
        #panel .amap-lib-driving {
            border-bottom-left-radius: 4px;
               border-bottom-right-radius: 4px;
            overflow: hidden;
        }
    </style>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.12&key=你的key值&plugin=AMap.Driving"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container"></div>
<div id="panel"></div>
<script type="text/javascript">
    //基本地圖加載
    var map = new AMap.Map("container", {
        resizeEnable: true,
        center: [116.397428, 39.90923],//地圖中心點
        zoom: 13 //地圖顯示的縮放級別
    });
    //構造路線導航類
    var driving = new AMap.Driving({
        map: map,
        panel: "panel"
    }); 
    // 根據起終點經緯度規劃駕車導航路線
    driving.search(new AMap.LngLat(116.379028, 39.865042), new AMap.LngLat(116.427281, 39.903719), function(status, result) {
        // result 便是對應的駕車導航信息,相關數據結構文檔請參考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
        if (status === 'complete') {
          console.log(result);
            log.success('繪製駕車路線完成');
            console.log(result);
            console.log(result.routes[0].distance);//獲取兩地之間的導航的實際距離(米)
            console.log(result.routes[0].time);//獲取兩地之間距離所須要的時間(分鐘)
        } else {
            log.error('獲取駕車數據失敗:' + result)
        }
    });
</script>
</body>
</html>

結果以下圖:css

相關文章
相關標籤/搜索