Sql 用于查找使用纬度和经度之间的距离 returns 错误的距离

Sql for finding the distance between using latitude and longitude returns wrong distance

我需要获取经纬度位置之间的距离, mysql 查询没有错误,但 returns 距离值错误

当我输入与数据库中相同的纬度和经度时,它给出的距离为“2590.4238273460855”而不是零,我不知道这有什么问题

mysql查询如下 这里的纬度和经度是我的 table 列名称

$sql = "SELECT id,(3956 * 2 * ASIN(SQRT( POWER(SIN(( $latitude - latitude) *  pi()/180 / 2), 2) +COS( $latitude * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( $longitude - longitude ) * pi()/180 / 2), 2) ))) as distance  
from table_name ORDER BY distance limit 100";

谁能帮帮我..

您的 Haversine 公式有误。 Haversine公式为:

Haversine_distance= r * 2 * ASIN(SQRT(a))

哪里

a = POW(SIN(latDiff / 2), 2) + COS(lat1) * COS(lat2) * POW(SIN(LonDiff / 2), 2)

因此,要更正您的代码,请将查询更改为:

"SELECT id,(3956 * 2 * ASIN(SQRT( POWER(SIN(( $latitude - latitude) / 2), 2) +COS( $latitude) * COS(latitude) * POWER(SIN(( $longitude - longitude ) / 2), 2) ))) as distance from table_name ORDER BY distance limit 100";