经纬度距离的计算

//javacript 
/*
*返回两点的距离(公里)
*@param _x1, _y1 经纬度坐标
*@param _x2, _y2 经纬度坐标 
*/
function getDistince(_x1, _y1, _x2, _y2){
	let x1 = _x1 * Math.PI / 180
	let x2 = _x2 * Math.PI / 180
	let y1 = _y1 * Math.PI / 180
	let y2 = _y2 * Math.PI / 180
    let tem = Math.cos(x1) * Math.cos(x2) * Math.cos(y2 - y1) + Math.sin(x1) *  Math.sin(x2)
    return Math.acos(tem) * 6731
}
//mysql 
* 查询数据表的point 字段 获得 距(x, y)点的距离 不大于distance 表项
*@param x, y 经纬度
*@param dis 最大距离
*/
let x, y;
let dis;
SELECT id, 6734 * acos( cos( radians(x) ) * cos( radians( X(point ) ) ) * cos( radians( Y(point) ) - radians(y) ) + sin( radians(x) ) * sin(radians( X(point) ) ) ) as distance 
from table_name 
where point != null
having distance <= dis
order by distance ASC 

你可能感兴趣的:(随笔)