2017-05-05 41 views
0

我有一張表,其中存儲了酒店的經緯度和位置。我希望實施的功能應該顯示從數據庫取得的靠近經度和緯度定義的給定點的酒店。如何獲取存儲在接近給定經度和緯度的數據庫中的酒店

控制器

 function check() 
     { 
     $this->load->model('hotel'); 
     $lat1=$this->input->post('lat'); 
     $lng1=$this->input->post('lng'); 
     $count['matched_data'] = $this->hotel->hoteldisplay($lat1,$lng1); 
     $checkdb =count($count['matched_data']); 
     } 
    Here is model: 
      function hoteldisplay($lat1,$lng1) 
      { 
      $this->db->select("landmark,(3959 * acos(cos(radians($lat1)) * 
      cos(radians(lat)) * cos(radians(longi) - rad`ians($lng1)+sin( 
      radians($lat1)) * sin(radians(lat)))) AS distance"); 
      $this->db->having('distance' <= 25);      
      $this->db->order_by('distance');      
      $this->db->limit(20, 0); 
      } 
+0

你的問題是什麼? –

+0

你的問題是什麼?你使用的是什麼框架? –

+0

認爲他正在使用基於格式@YazanWYusuf的codeigniter ....但即時猜測他的控制器模型是正確的,他想爲該標記創建一個客戶端ajax事件函數..當標記被固定時,它應該發佈lat並長,並返回響應並更新地圖 –

回答

0

您需要適用於您的SQL查詢的半正矢距離。這是一個例子:

SELECT z.zip, 
     z.primary_city, 
     z.latitude, z.longitude, 
     p.distance_unit 
       * DEGREES(ACOS(COS(RADIANS(p.latpoint)) 
       * COS(RADIANS(z.latitude)) 
       * COS(RADIANS(p.longpoint) - RADIANS(z.longitude)) 
       + SIN(RADIANS(p.latpoint)) 
       * SIN(RADIANS(z.latitude)))) AS distance_in_km 
    FROM zip AS z 
    JOIN ( /* these are the query parameters */ 
     SELECT 42.81 AS latpoint, -70.81 AS longpoint, 
       50.0 AS radius,  111.045 AS distance_unit 
    ) AS p ON 1=1 
    WHERE z.latitude 
    BETWEEN p.latpoint - (p.radius/p.distance_unit) 
     AND p.latpoint + (p.radius/p.distance_unit) 
    AND z.longitude 
    BETWEEN p.longpoint - (p.radius/(p.distance_unit * COS(RADIANS(p.latpoint)))) 
     AND p.longpoint + (p.radius/(p.distance_unit * COS(RADIANS(p.latpoint)))) 
    ORDER BY distance_in_km 
LIMIT 15 

摘自here。至於代碼點火器,你也可以在那裏應用,參見here

相關問題