我会修改the answer from gabrielk 和the linked blog post 通过使用database indexes 和minimizing the number of actual distance calculations.
如果知道用户的坐标和最大距离(比如10km),可以绘制一个20km×20km的边界框,当前位置在中间。获取这些边界坐标并query only stores between these latitudes and longitudes. 不要在数据库查询中使用三角函数,因为这将阻止使用索引。(因此,如果商店位于边界框的东北角,您可能会在离您12公里的地方找到一家商店,但我们会在下一步将其扔掉。)
只计算退回的几家店铺的距离(当鸟儿飞起来时,或者根据您的喜好计算实际的驾驶方向)。如果您有大量商店,这将大大缩短处理时间。
对于相关的查找(“给出十个最近的商店”),您可以进行类似的查找,但要有一个初始距离猜测(因此您从10km×10km的区域开始,如果没有足够的商店,则将其扩展到20km×20km,以此类推)。对于这个初始距离,您可以计算一次总面积上的门店数量,然后使用它。或者记录所需查询的数量,并随着时间的推移进行调整。
我添加了一个full code example at Mike\'s related question, 这里有一个扩展,可以提供最接近的X位置(快速且几乎未经测试):
class Monkeyman_Geo_ClosestX extends Monkeyman_Geo
{
public static $closestXStartDistanceKm = 10;
public static $closestXMaxDistanceKm = 1000; // Don\'t search beyond this
public function addAdminPages()
{
parent::addAdminPages();
add_management_page( \'Location closest test\', \'Location closest test\', \'edit_posts\', __FILE__ . \'closesttest\', array(&$this, \'doClosestTestPage\'));
}
public function doClosestTestPage()
{
if (!array_key_exists(\'search\', $_REQUEST)) {
$default_lat = ini_get(\'date.default_latitude\');
$default_lon = ini_get(\'date.default_longitude\');
echo <<<EOF
<form action="" method="post">
<p>Number of posts: <input size="5" name="post_count" value="10"/></p>
<p>Center latitude: <input size="10" name="center_lat" value="{$default_lat}"/>
<br/>Center longitude: <input size="10" name="center_lon" value="{$default_lon}"/></p>
<p><input type="submit" name="search" value="Search!"/></p>
</form>
EOF;
return;
}
$post_count = intval($_REQUEST[\'post_count\']);
$center_lon = floatval($_REQUEST[\'center_lon\']);
$center_lat = floatval($_REQUEST[\'center_lat\']);
var_dump(self::getClosestXPosts($center_lon, $center_lat, $post_count));
}
/**
* Get the closest X posts to a given location
*
* This might return more than X results, and never more than
* self::$closestXMaxDistanceKm away (to prevent endless searching)
* The results are sorted by distance
*
* The algorithm starts with all locations no further than
* self::$closestXStartDistanceKm, and then grows this area
* (by doubling the distance) until enough matches are found.
*
* The number of expensive calculations should be minimized.
*/
public static function getClosestXPosts($center_lon, $center_lat, $post_count)
{
$search_distance = self::$closestXStartDistanceKm;
$close_posts = array();
while (count($close_posts) < $post_count && $search_distance < self::$closestXMaxDistanceKm) {
list($north_lat, $east_lon, $south_lat, $west_lon) = self::getBoundingBox($center_lat, $center_lon, $search_distance);
$geo_posts = self::getPostsInBoundingBox($north_lat, $east_lon, $south_lat, $west_lon);
foreach ($geo_posts as $geo_post) {
if (array_key_exists($geo_post->post_id, $close_posts)) {
continue;
}
$post_lat = floatval($geo_post->lat);
$post_lon = floatval($geo_post->lon);
$post_distance = self::calculateDistanceKm($center_lat, $center_lon, $post_lat, $post_lon);
if ($post_distance < $search_distance) {
// Only include those that are in the the circle radius, not bounding box, otherwise we might miss some closer in the next step
$close_posts[$geo_post->post_id] = $post_distance;
}
}
$search_distance *= 2;
}
asort($close_posts);
return $close_posts;
}
}
$monkeyman_Geo_ClosestX_instace = new Monkeyman_Geo_ClosestX();