下面的代碼是我寫的,用於在搜索到的郵政編碼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);
使用haversine公式,https://developers.google.com/maps/articles/phpsqlsearch_v3。請參閱'使用MySQL查找位置「。 – chris85
如果我將Sql語句限制爲1,它會給我最近的位置嗎? – user2593040
'Limit'和'order'的距離。 – chris85