2011-11-24 66 views
1

我正在使用geokit-rails3 gem在特定大學範圍內的所有學院中查找產品。一所大學有多種產品和一種產品屬於大學,還有另一種產品和產品屬於類別的分類模式。但是,當我嘗試從使用geokit的地址的數據庫中找到大學時,它告訴我在我的表格中缺少lat coloumn。Geokit plus Rails 3.1.1,經緯度問題。

院校遷移

create_table :colleges do |t| 
    t.string :name 
    t.text :address 
    t.string :city 
    t.string :state 
    t.integer :zipcode 

    t.timestamps 

控制器

 @products = College.within(5, :origin=>@current_product.college.address).product 

錯誤:

 Mysql::Error: Unknown column 'colleges.lat' in 'field list': SELECT `colleges`.*, 
     (ACOS(least(1,COS(0.3223824452162744)*COS(1.2891920858347756)*COS(RADIANS(colleges.lat))*COS(RADIANS(colleges.lng))+ 
     COS(0.3223824452162744)*SIN(1.2891920858347756)*COS(RADIANS(colleges.lat))*SIN(RADIANS(colleges.lng))+ 
     SIN(0.3223824452162744)*SIN(RADIANS(colleges.lat))))*3963.19) 
     AS distance FROM `colleges` WHERE ((colleges.lat>18.398868573573203 AND colleges.lat<18.543438426426793 AND colleges.lng>73.78905443427034 AND colleges.lng<73.94147656572967)) AND ((
     (ACOS(least(1,COS(0.3223824452162744)*COS(1.2891920858347756)*COS(RADIANS(colleges.lat))*COS(RADIANS(colleges.lng))+ 
     COS(0.3223824452162744)*SIN(1.2891920858347756)*COS(RADIANS(colleges.lat))*SIN(RADIANS(colleges.lng))+ 
     SIN(0.3223824452162744)*SIN(RADIANS(colleges.lat))))*3963.19) 
     <= 5)) 

任何暗示該如何解決這個問題呢?

+0

您是否有lat列?看來geokit-rails3不會爲你創建一個。 – tommasop

+0

在這種情況下,我沒有緯度列 –

回答

3

凡是與geokit acts_as_mappable需要LAT & LNT領域(這些可以使用不同的名稱來覆蓋)

看到的頂部:http://geokit.rubyforge.org/readme.html

我建議:

add_column :colleges, :lat, :float 
add_column :colleges, :lng, :float 
add_index :colleges, [:lat, :lng] 

此外,要自動更新地址:

before_save :update_location 

def update_location 
#you probably you to use a full address, or join all the address parts instead of just self.address 
loc=Geocoder.geocode(self.address) 
if loc.success 
    self.lat = loc.lat 
    self.lng = loc.lng 
end 
end 
+0

當我創建新的大學時,是否需要在數據庫中手動添加lat和lng值? –

+1

是的,但您可以自動執行此操作,我會更新上述評論 – easyjo

0

您也可以有不同的列並將它們映射到acts_as_mappable,如下所示:

acts_as_mappable :default_units => :miles, 
        :default_formula => :sphere, 
        :distance_field_name => :distance, 
        :lat_column_name => :lat, 
        :lng_column_name => :lng