2 つの都市間の距離を計算する代わりに、100 マイルの境界ボックスを計算すると、データベースにプラグインする 4 つの float 変数があります。float 比較は、データベース内の距離計算よりもはるかに高速です。欠点は、コーナーでの距離が少し長くなることです。
境界ボックスを計算する PHP 関数
function getBoundingBox($lat_degrees,$lon_degrees,$distance_in_miles)
{
$半径 = 3963.1; //地球のマイル
// ベアリング
$due_north = 0;
$due_south = 180;
$due_east = 90;
$当然_西= 270;
// 緯度と経度をラジアンに変換
$lat_r = deg2rad($lat_degrees);
$lon_r = deg2rad($lon_degrees);
// $distance_in_miles 離れた最北端、最南端、最東端、最西端の角を見つける
//元の式から
// http://www.movable-type.co.uk/scripts/latlong.html
$northmost = asin(sin($lat_r) * cos($distance_in_miles/$radius) + cos($lat_r) * sin ($distance_in_miles/$radius) * cos($due_north));
$southmost = asin(sin($lat_r) * cos($distance_in_miles/$radius) + cos($lat_r) * sin ($distance_in_miles/$radius) * cos($due_south));
$eastmost = $lon_r + atan2(sin($due_east)*sin($distance_in_miles/$radius)*cos($lat_r),cos($distance_in_miles/$radius)-sin($lat_r)*sin($lat_r)) ;
$westmost = $lon_r + atan2(sin($due_west)*sin($distance_in_miles/$radius)*cos($lat_r),cos($distance_in_miles/$radius)-sin($lat_r)*sin($lat_r)) ;
$northmost = rad2deg($northmost);
$southmost = rad2deg($southmost);
$eastmost = rad2deg($eastmost);
$westmost = rad2deg($westmost);
//NWコーナーとSEコーナーの2点を返す
配列を返します($northmost,$westmost,$southmost,$eastmost);
}
あなたのSQLは
SELECT * FROM table WHERE latitude <= $northmost AND longitude >= $westmost AND latitude >= $southmost AND longitude <= $eastmost