简体   繁体   English

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

[英]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.

相关问题 如何使用php使用经度和纬度获取点之间的距离? - how to get distance between points using latitude and longitude using php? Laravel中两个位置之间的距离在数据库查询中使用经纬度 - Distance between two locations in Laravel using latitude and longitude in database query 街道和点之间的距离(如果是经度和纬度) - Distance between street and point, if latitude and longitude php:根据中心纬度/经度和距离查找纬度和经度边界 - php: finding latitude and longitude boundaries based on a central lat/lng and distance Google Distance Api以特定的纬度经度返回零 - Google Distance Api returns Zero with specific Latitude Longitude 如何在两对纬度经度之间以几米为单位获得距离 - How to get distance in few meters between 2 pair of latitude longitude 如何使用经度和纬度计算餐厅与该餐厅附近的 10 个司机之间的距离? - How to calculate the distance between a restaurant and 10 drivers who are nearby to that restaurant restaurant using longitude and latitude? 使用 PHP 和谷歌地图 Api 计算出 2 个纬度和经度之间的距离 - Using PHP and google Maps Api to work out distance between 2 latitude and longitude SQL选择用户并根据经度纬度过滤距离 - SQL Select the users and filter distance based on longitude latitude 使用纬度和经度按距离存储和分类Symfony2学说 - Store and sort by distance using latitude and longitude Symfony2 Doctrine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM