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

问题描述 投票:3回答:1

我需要得到纬度和经度的位置之间的距离,mysql查询工作没有错误,但它返回错误的距离值

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

mysql查询如下所示,纬度和经度是我的表列名

$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";

有人可以帮我吗...

php mysql location latitude-longitude
1个回答
2
投票

您的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";
© www.soinside.com 2019 - 2024. All rights reserved.