[英]Sql for finding the distance between using latitude and longitude returns wrong distance
i need to get the distance between location of the latitude and longitude, mysql query works without error but it returns the wrong distance value 我需要得到纬度和经度的位置之间的距离,mysql查询工作没有错误,但它返回错误的距离值
When i enter the same latitude and longitude as in the database it gives the distance as "2590.4238273460855" instead of zero, i dont known whats wrong in this 当我输入与数据库中相同的纬度和经度时,它给出的距离为“2590.4238273460855”而不是零,我不知道这是错误的
mysql query is as given below here latitude and longitude are my table column name 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";
can anyone help me please.. 有人可以帮我吗...
There is an error in your Haversine formula. 您的Haversine公式中存在错误。 Haversine formula is:
Haversine配方是:
Haversine_distance= r * 2 * ASIN(SQRT(a))
Haversine_distance = r * 2 * ASIN(SQRT(a))
where 哪里
a = POW(SIN(latDiff / 2), 2) + COS(lat1) * COS(lat2) * POW(SIN(LonDiff / 2), 2)
a = POW(SIN(latDiff / 2),2)+ COS(lat1)* COS(lat2)* POW(SIN(LonDiff / 2),2)
Therefore, to correct your code change your query to: 因此,要更正代码,请将查询更改为:
"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";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.