2017-01-24 56 views
0

下面的代碼是我寫的,用於在搜索到的郵政編碼50英里內進行半徑搜索。我如何更改此代碼以僅查找最近的診所位置,而不是診所位置列表?因此,如果我爲科羅拉多州的丹佛輸入郵政編碼,而且我的位置主要在紐約,賓夕法尼亞州和俄亥俄州,那麼我如何能夠找到位於俄亥俄州的最近位置?在PHP/MYSQL中查找最近的郵政編碼位置

/* RADIUS SEARCH - 50 MILES FROM THE ZIP CODE VARIABLE */ 
$zipcode=trim($_REQUEST['zipcode']); 


//Find Out The Longitude & Latitude of Zipcode in existing database 
$zcquery = "SELECT * FROM wp_nava5jxxyp_zipcodes WHERE zipcode = $zipcode"; 
    $result = mysqli_query($dbc, $zcquery); 
    $row = mysqli_fetch_array($result); 

    $lat = $row['latitude']; 
    $long = $row['longitude']; 


//Setting the Default Distance 50 Miles 
// (Equation 1/69 = 0.0144927536231884 * 50 miles equals result below) 
$miles = 0.7246376811594203; 

    /* Query To Search for all of the Zip Codes within the range */ 
    $query = 'SELECT zipcode from wp_nava5jxxyp_zipcodes 
    WHERE latitude between ' . $lat . ' - ' . $miles . ' and ' . $lat . ' + ' . $miles . ' 
    and longitude between ' . $long . ' - ' . $miles . ' and ' . $long . ' + ' . $miles; 
    $result = mysqli_query($dbc, $query); 


    //Put those zip codes into a variable array 
    $variabele = array(); 
while($row = mysqli_fetch_array($result)) 
{ 
if($row['zipcode'] != '') 
    $variabele[] = $row['zipcode']; 
} 
$zipcodelist = implode(', ', $variabele); 


// Close Connection 
mysqli_close($dbc); 

//Query Database For Any Clinics that are included in zip code list 
$args = array(
'posts_per_page' => 10, 
'orderby'   => 'post_title', 
'order'   => 'DESC', 
'post_type' => 'clinics', 
    'meta_query' => array (
     array (
      'key' => 'cliniczipcode', 
      'value' => $zipcodelist, 
      'compare' => 'IN' 
     ) 
    )); 

// the query 
$the_query = new WP_Query($args); 
+0

使用haversine公式,https://developers.google.com/maps/articles/phpsqlsearch_v3。請參閱'使用MySQL查找位置「。 – chris85

+0

如果我將Sql語句限制爲1,它會給我最近的位置嗎? – user2593040

+0

'Limit'和'order'的距離。 – chris85

回答

0

看到這個Fastest Way to Find Distance Between Two Lat/Long Points

基本上你計算距離你的基地latlon,排序最短距離,並獲得最高的項目。如果您不想打擾複雜的查詢,也可以在代碼中執行此操作。

關鍵詞是「大圈距離」和「haversine公式」

編輯

比方說,我有獸醫診所的數據庫在洛杉磯

Limehouse Veterinary Clinic 34.1534500122 -118.3630599976 
LA Pet Clinic     34.0838623047 -118.3284454346 
Bestfriends Animal Hospital 34.1692466736 -118.3970489502 

而我的基礎座標是好萊塢緯度34.092324,長度-118.337122

haversine公式需要弧度而不是度數的座標。

有360度pi第2個弧度所以 Ŷ弧度= 6.28319 * X度/ 360(求解Y)

以下是haversine公式其中r2 r arcsin是地球的半徑:6371000米

-- haversine 
--       latdif         londif 
-- 2 r arcsin [ sqrt[ sin^2(------) + cos(lat1) * cos(lat2) * sin^2(------) ] ] 
--       2          2 

因此,要接近我的位置最近的診所,我可以用這個查詢作爲上述公式的翻譯

select 
name, 
2 * 6371000 * ASIN(
    sqrt(
     SIN(6.28319 * (34.092324 - lat)/360/2) * SIN(6.28319 * (34.092324 - lat)/360/2) 
     + 
     COS(6.28319 * 34.092324/360 ) * COS(6.28319 * lat/360 ) 
     * 
     SIN(6.28319 * (-118.337122 - lon)/360/2) * SIN(6.28319 * (-118.337122 - lon)/360/2) 
    ) 
) 
as dist_in_meters 
from locations ORDER BY dist_in_meters; 

給予我

name        dist_in_meters 
LA Pet Clinic     1234.3897096551932 
Limehouse Veterinary Clinic  7204.075434291492 
Bestfriends Animal Hospital 10177.689847331932 
+0

我怎樣才能確定第二個位置?我知道丹佛的長/拉特,但是對於我的數據庫中的郵政編碼列表,我怎麼知道哪一個用作第二點?圖從丹佛到「?」的距離 – user2593040

+0

您需要診所的實際座標。您可以使用Google地圖手動查找這些內容。然後,您將比較您的基本郵政編碼座標與這些座標。理想情況下,郵政編碼座標位於覆蓋區域的中心。但在真實應用程序中,您必須使用用戶的實際位置(使用移動設備的GPS或瀏覽器的地理位置功能)與診所的位置進行比較 –

+0

好的我在位置數據庫中有這些字段,我只是困惑我如何才能改變我的代碼。我甚至可能無法使用我現有的任何代碼。 – user2593040

相關問題