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

版权声明:本文为博主原创文章,未经博主允许不得转载。如需开发微信小程序可加微信: 13977284413 https://blog.csdn.net/qq_35713752/article/details/84986826

html

<!DOCTYPE html>
<html>

	<head>
		<meta 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
				})
			}
		</script>
	</body>

</html>

猜你喜欢

转载自blog.csdn.net/qq_35713752/article/details/84986826