如何计算在php中与经纬度的距离? [英] How to calculate distance from lat/long in php?
本文介绍了如何计算在php中与经纬度的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做的是我在数据库中有一些条目,这些条目存储着经/纬度.我想计算用户纬度/经度和条目纬度/经度(在数据库中)之间的距离.之后,我想回显距离小于500米的那些.到目前为止,我可以使用foreach
做到这一点.
What I am trying to do is I have entries in the database which have a lat/long stored with them. I want to calculate the distance between users lat/long and entries lat/long (in DB). After that, I want to echo the ones with distance less than 500 meters. So far I am able to do this using foreach
.
<?php
mysql_connect("localhost", "beepbee_kunwarh", "kunwar") or die('MySQL Error.');
mysql_select_db("beepbee_demotest") or die('MySQL Error.');
$Lat = $_REQUEST['Lat'];
$long = $_REQUEST['long'];
$query = mysql_query("SELECT a.*, 3956 * 2 * ASIN(SQRT( POWER(SIN(($Lat - Lat) * pi()/180 / 2), 2) + COS($Lat * pi()/180) * COS(Lat * pi()/180) *POWER(SIN(($long - long) * pi()/180 / 2), 2) )) as distance FROM userResponse GROUP BY beepid HAVING distance <= 500 ORDER by distance ASC;");
$data = array();
while ($row = mysql_fetch_array($query)) {
$data[] = $row;
}
echo json_encode($data);
?>
推荐答案
我是几周前完成的.
此链接是您最好的选择:
This link is your best bet:
http://code.google.com/apis/maps/articles/phpsqlsearch.html
即使您不使用他们的API,他们的PHP和SQL查询也非常有用.
Even if you don't use their API, their PHP and SQL query helped really well.
这篇关于如何计算在php中与经纬度的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文