0

我正在开发一个房地产网络目录,并希望使用 geokit gem 对每个广告进行地理编码。我的问题是,如果我想按国家、所选国家的城市、行政区域或所选城市最近的地铁站进行搜索,那么从性能点来看,最好的数据库布局是什么。可用的国家、城市、行政区域和地铁站应由目录管理员定义,并且必须通过地理编码进行验证。

我想出了单表:

  create_table "geo_locations", :force => true do |t|
    t.integer "geo_location_id"                     #parent geo location (ex. country is parent geo location of city  
    t.string  "country",             :null => false #necessary for any geo location
    t.string  "city",                               #not null for city geo location and it's children
    t.string  "administrative_area"                 #not null for administrative_area geo location and it's children
    t.string  "thoroughfare_name"                   #not null for metro station or street name geo location and it's children
    t.string  "premise_number"                      #house number
    t.float   "lng",                 :null => false 
    t.float   "lat",                 :null => false
    t.float   "bound_sw_lat",        :null => false
    t.float   "bound_sw_lng",        :null => false
    t.float   "bound_ne_lat",        :null => false
    t.float   "bound_ne_lng",        :null => false
    t.integer "mappable_id"
    t.string  "mappable_type"
    t.string  "type"                                #country, city, administrative area, metro station or address 
  end

最终地理位置是地址,它包含在地图上放置房地产广告标记的所有必要信息。但我仍然坚持搜索功能。

任何帮助将不胜感激。

4

1 回答 1

1

您可能想看看 Thinking Sphinx,它支持内置的地理搜索。这就是我的做法:

class Company < ActiveRecord::Base
    define_index do
        indexes :name, :sortable => true
        has 'RADIANS(lat)',  :as => :latitude,  :type => :float
        has 'RADIANS(lng)', :as => :longitude, :type => :float
    end
end
# Searching for companies, sort by closest first
Company.search "bananas", :geo => [lat, lng], :order => "@geodist ASC, @relevance DESC"
于 2010-06-14T21:50:38.937 回答