最近公司需要实现一个校验手机位置的经纬度,是不是在楼栋的经纬度范围的需求
记录一下cv知识
// 获取两个经纬度的距离
// 校验数据:A点:(112.91646300,28.17454050)B点:(112.94490050,28.20267220)直线距离约为:4.176km
//let A = {longitude: 112.916463,latitude: 28.1745405};
//let B = {longitude: 112.94490050,latitude: 28.20267220};
const getDisance = function(current, building) {
let a,
b,
R = 6378137; //R:地球半径
current.latitude = (current.latitude * Math.PI) / 180.0;
building.latitude = (building.latitude * Math.PI) / 180.0;
a = current.latitude - building.latitude; //纬度
b = ((current.longitude - building.longitude) * Math.PI) / 180.0;
let d, sa2, sb2;
sa2 = Math.sin(a / 2.0);
sb2 = Math.sin(b / 2.0);
d =
2 *
R *
Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(current.latitude) * Math.cos(building.latitude) * sb2 * sb2));
return d;//返回:XXX米
}