2016-12-10 11 views
1

場所の近くを検索する検索バーを作成しようとしています。今のところそれはハードコードされています。このステートメントは、経度と緯度のようなステートメントを追加するまで機能します。これを行うより良い方法はありますか?SQL検索 "near"経度と緯度

$searchString = explode(' ', $_GET["searchString"]); 
$useLocation = $_GET["useCurrentLocation"]; 
if($useLocation == 1){ 
    foreach($searchString AS $key){ 
     $stmt = $db->prepare("SELECT * FROM locations WHERE Name LIKE '%{$key}%' AND Longitude Like '-79.8579' And Latitude Like '43.5237'"); 
     $stmt->execute(); 
     $result = $stmt->get_result(); 
     while($row = $result->fetch_assoc()){ 
      echo $row['Name']; 
     } 

    } 
} 

答えて

0

あなたはStackOverflowの上のいくつかの答えがあります

"どの場所で検索する" について、インターネット上で多くの解決策を見つけることができます。

やGoogleで:ここで

はそのことについて私の最後のクエリです:

私はGoogle MapsのAPIを経由して緯度/経度を取得します。住所、 を入力し、GPS座標を取得します。 の後、SQLで位置とオブジェクトを計算する

$ longitude_ref = 7.694731; $ latitude_ref = 48.560719;

SELECT * 
    FROM maps 
    WHERE (
    6366 * 
    acos(
     cos(radians(48.560719)) -- $latitude_ref 
     * 
     cos(radians(`latitude`)) 
     * 
     cos(radians(`longitude`) - radians($longitude_ref)) -- $longitude_ref 
     + 
     sin(radians(48.560719)) * sin(radians(`latitude`)) -- $latitude_ref 
    ) 
) <= 2 -- r in Km (here: 2 km) 

また、2点間の距離を計算することができますインターネット上

SELECT pg.id, pg.*, pg1.*, 6366 * 2 * 
    atan2(
     sqrt(
     SIN((RADIANS(pg1.latitude) - RADIANS(pg.latitude))/2) * SIN((RADIANS(pg1.latitude) - RADIANS(pg.latitude))/2) + COS(RADIANS(pg.latitude)) * COS(RADIANS(pg1.latitude)) * SIN((RADIANS(pg1.longitude) - RADIANS(pg.longitude))/2) * SIN((RADIANS(pg1.longitude) - RADIANS(pg.longitude))/2)  
    ), 
     sqrt(
     1 - SIN((RADIANS(pg1.latitude) - RADIANS(pg.latitude))/2) * SIN((RADIANS(pg1.latitude) - RADIANS(pg.latitude))/2) + COS(RADIANS(pg.latitude)) * COS(RADIANS(pg1.latitude)) * SIN((RADIANS(pg1.longitude) - RADIANS(pg.longitude))/2) * SIN((RADIANS(pg1.longitude) - RADIANS(pg.longitude))/2) 
    ) 
    ) as calculated_distance_final_km 
    FROM perso_gps pg 
    WHERE pg.calculated_dist IS NULL 

多くの例:)