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">
<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&key=WFLBZ-ABRHX-A474W-75TT3-L2NZF-VAGAC"></script>
</head>
<body>
<button onclick="aaa()">点击</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
})
}