2016-03-29 17 views
3

10キロメートル以内の任意の場所を検索したい緯度と経度を使用します。私はどのように制限を設定できますか?ここに私のSQLクエリは次のとおりです。sqlでlatとlongを使用して距離を見つける

$this->db->select(", (6371 acos(cos(radians(37)) cos(radians(latitude)) cos(radians(longtitude)- radians($lng)) + sin(radians($lat)) * sin(radians(latitude)))) AS distance");       
$this->db->from('table_name'); 
$this->db->order_by('distance'); 
+0

あなたはどんなエラーを得たことがありますか? – Sadikhasan

+0

$ this-> db-> having( 'distance <10'); selectステートメントが正しいと仮定し、距離をキロメートルで示します – shantanu

+0

ここで私の計算式は10です。20x20の境界矩形(正方形)を使用して結果をすばやくフィルタリングできます – Strawberry

答えて

0
Try this: 
$this->db->select(", (6371 acos(cos(radians(37)) cos(radians(latitude)) cos(radians(longtitude)- radians($lng)) + sin(radians($lat)) * sin(radians(latitude)))) AS distance");       
$this->db->from('table_name'); 
$this->db->having('distance<',10) 
$this->db->order_by('distance'); 
+0

まだありませんkmsの結果を得る。 – Ritu

+0

あなたはKmsに入らないことを意味しますか?結果やエラーが出ていますか? – Thejas

+0

私は結果を得ていますが、間違った結果を得ています。上記のクエリでは、距離フィールドでいくつかのレコードを取得しています。このフィールドを使用してクエリの結果を取得します。 「距離<10」を使用している場合は何も表示されず、「距離<2181」を使用すると結果が表示される場合を意味します。 ですが、結果をkmsまたはマイルで検索したいと考えています。 – Ritu

3

はこれを試してみてください:

$this->db->query(' SELECT (6371 * acos(cos(radians(37)) cos(radians(latitude)) cos(radians(longtitude)- radians("'.$lng.'")) + sin(radians("'.$lat.'")) * sin(radians(latitude)))) AS distance 
         FROM table_name 
         HAVING distance < 10 
         ORDER BY distance ASC'); 
0
$sql = 'select *,(((acos(sin(('.$lat.'*pi()/180)) * 
      sin((loc_LAT_centroid*pi()/180))+cos(('.$lat.'*pi()/180)) * 
      cos((loc_LAT_centroid*pi()/180)) * cos((('.$lang.'-loc_LONG_centroid)* 
      pi()/180))))*180/pi())*60*1.1515 
     ) AS distance from table_name order by distance'; 

$lat = latitude of your current location 
$lang = longitude of your current location 

loc_LAT_centroid = field name of your table where you store the latitude of the places. 
loc_LONG_centroid = field name of your table where you store the longitude of the places. 
関連する問題