使用地理定位,按距离排序结果


Using geolocation, sort results by distance

嗨,我想为我的平台(http://carrotleads.com)改进我的"按地理位置搜索"。

我能够接收到登陆我网站的用户的地理位置。使用他的IP,我可以得到下面的JSONhttp://www.freegeoip.net/json/这并不总是一致的,因为有时有些字段没有被填充。

我的数据库有客户记录,引用他们的操作领域。

如何计算用户到每个客户的距离并按距离升序对搜索结果排序(最接近的客户/实体首先显示)

为例:http://farmersmarkets.org.au/markets

按字母顺序显示我所在州的所有市场。如何显示结果,按距离排序。

我在LAMP堆栈上编写代码,但很高兴看到其他语言的示例并适应它。

很有趣的问题。
做了一些搜索,我发现了这篇很酷的文章:http://aravindtrue.wordpress.com/2009/06/30/calculate-distance-using-latitude-and-longitude-php-mysql/

作者在PHP中发布了一个函数,用于使用经度和纬度查找两点之间的距离。这真的很方便,因为您列出的freegeoip api返回long/lat。

function getDistanceBetweenPointsNew($latitude1, $longitude1,
$latitude2, $longitude2, $unit = 'Mi')
{
   $theta = $longitude1 - $longitude2;
   $distance = (sin(deg2rad($latitude1)) *
   sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) *
   cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
   $distance = acos($distance);
   $distance = rad2deg($distance);
   $distance = $distance * 60 * 1.1515;
   switch($unit)
   {
      case 'Mi': break;
      case 'Km' : $distance = $distance *1.609344;
   }
   return (round($distance,2));
}

他还列出了一个示例SQL查询,该查询将返回距离

内的结果。
$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * 
sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * 
cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*
pi()/180))))*180/pi())*60*1.1515) as distance FROM `MyTable` 
WHERE distance <= ".$distance."

一定要看看那篇文章,我认为它包含了你要找的所有信息。