2016-05-17 124 views
1

我有一個mySQL表中的項目,緯度&經度等
我將如何去搜索KM中的某些數量的項目基於他們目前的地理位置?搜索用戶距離內的項目(地理位置)PHP/MySQL

例子:
用戶選擇從下拉框中的值「20公里」
我怎麼會得到基於其地理位置20公里距離內的所有項目?

截圖下面佈局的:

Search button

+1

航路或公路上的距離?它對於在你的位置附近找到位置很重要。 –

+0

@hosseinbarzegari通過ROAD的距離。 – deluxenathan

+0

最好的解決方案是使用谷歌API,它的幫助你和它的路徑更新。儘管我發送一個答案samplel代碼與源給你。 ;) –

回答

0

其最好使用谷歌樣本代碼。

此鏈接可以幫助你:

http://code.google.com/apis/maps/articles/phpsqlsearch.html

他們的PHP和SQL查詢幫助真的很好。

地址:

this code還可以幫助:

function distance($lat1, $lon1, $lat2, $lon2, $unit) { 

    $theta = $lon1 - $lon2; 
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
    $dist = acos($dist); 
    $dist = rad2deg($dist); 
    $miles = $dist * 60 * 1.1515; 
    $unit = strtoupper($unit); 

    if ($unit == "K") { 
     return ($miles * 1.609344); 
    } else if ($unit == "N") { 
     return ($miles * 0.8684); 
    } else { 
     return $miles; 
    } 
} 

結果:

echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>"; 
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>"; 
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>"; 
0

你可以計算出當前的位置,所有公園之間海峽的距離和過濾器僅匹配距離標準。

/** 
    * @param float $parkLatitude 
    * @param float $parkLongitude 
    * @param float $userLatitude 
    * @param float $userLongitude 
    * @param float $distance in km 
    * 
    * @return bool 
    */ 
    function isParkInDistance($parkLatitude, $parkLongitude, $userLatitude, $userLongitude, $distance) 
{ 
    $earthRadius = 6371; //km 

    $dLat = deg2rad($userLatitude - $parkLatitude); 
    $dLon = deg2rad($userLongitude - $parkLongitude); 

    $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($parkLatitude)) * cos(deg2rad($userLatitude)) * sin($dLon/2) * sin($dLon/2); 
    $c = 2 * asin(sqrt($a)); 
    $d = $earthRadius * $c; 

    return $d <= $distance; 
} 

或者你可以使用像this google service通過道路

2
SELECT *,3956*2*ASIN(SQRT(POWER(SIN((19.286558 - latitude)*pi()/180/2),2)+COS(19.286558 * pi()/180) 
*COS(latitude * pi()/180)*POWER(SIN((-99.612494 -longitude)* pi()/180/2),2))) 
as distance FROM table having distance < 10 ORDER BY distance; 

這將會給你10公里範圍內內的記錄來計算距離。在子句中修改20米的查詢。