冒泡排序实例及地图计算两点距离
实际应用场景
开发端:wepy小程序
业务要求:实现根据坐标点计算商户与用户的距离,且将商户由远到近进行排序
技术要点:
- 根据坐标点计算商户与用户的距离
- 商户由远到近排序
两点距离
注意:该实例用小程序的地图,是腾讯地图,非百度坐标系,不清楚百度是否可以正确计算出距离。
实现如下:
async getLocation (lat, lon, locationRes) { // lat 经度; lon 纬度, locationRes:当前经纬度对象
var La1 = lat * Math.PI / 180.0
var La2 = locationRes.latitude * Math.PI / 180.0
var La3 = La1 - La2
var Lb3 = lon * Math.PI / 180.0 - locationRes.longitude * Math.PI / 180.0
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)))
s = s * 6378.137
s = Math.round(s * 10000) / 10000
if (s < 1) { // 假如小于1 说明小于1km,单位为m
// return s * 1000 + 'm'