JS根据两点的经纬度坐标得到驾车行驶距离

前端之家收集整理的这篇文章主要介绍了JS根据两点的经纬度坐标得到驾车行驶距离前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

html

<pre class="has">
<!DOCTYPE html>

<head>
    <<a href="/tag/Meta/" target="_blank" class="keywords">Meta</a> http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&amp;key=WFLBZ-ABRHX-A474W-75TT3-L2NZF-VAGAC"&gt;</script>
</head>

<body>
    <button onclick="aaa()"&gt;点击</button>
    <script>
        function aaa() {
            get_distance('21.660388,110.938697','21.66701,110.92245').then(function(resolve_finish) {
                console.log('距离:' + resolve_finish)
            })
        }

        function get_distance(s,e) {
            return myPromise = new Promise(function(resolve,reject) {
                var my_distance;
                var directionsService = new qq.maps.DrivingService({

complete: function(response) {
var distance = JSON.stringify(response.detail.routes[0].distance);
my_distance = distance + '米';
resolve(my_distance);
}
});
var start = s.split(","); //起点经纬度
var end = e.split(","); //目的地经纬度
directionsService.setPolicy(qq.maps.DrivingPolicy['LEAST_DISTANCE']);//"LEAST_TIME">最少时间 LEAST_DISTANCE">最短距离 "AVOID_HIGHWAYS">避开高速
directionsService.search(new qq.maps.LatLng(start[0],start[1]),new qq.maps.LatLng(end[0],end[1]));
}).then(function(resolve_finish) {
return resolve_finish
})
}

猜你在找的JavaScript相关文章