2011-05-29 61 views
0

我已经成功地获得了思维狮身人面像与关联模型上的地理定位一起工作。快乐的时光!思维狮身人面像 - 显示该协会的正确记录

但我现在需要它在Google地图上显示正确的关联记录。

该情景是has_many办事处的公司。办公室已经获得了lng,lat值。我正在寻找公司,并将办事处与它联系起来。

例如

define_index do 

indexes :name, :sortable => true 
indexes offices(:city), :as => :city 
indexes offices(:postal_code), :as => :postal_code 

has "RADIANS(offices.lat)", :as => :lat, :type => :float 
has "RADIANS(offices.lng)", :as => :lng, :type => :float 

has created_at 
has updated_at 

set_property :latitude_attr => 'lat' 
set_property :longitude_attr => 'lng' 
set_property :field_weights => { 'name'  => 10, 
            'service_name' => 9, 
            'city' => 8 } 

end 

在y位置为X公司搜索/邮编完美的作品,显示出正确的公司已经在@geodist半径内得到所需的位置设有办事处。

例如

{:geo=>[0.9283660690549609, -0.050527407508941975], :sort_mode=>:expr, :sort_by=>"@weight * @weight/@geodist", :with=>{"@geodist"=>0.0..120700.8}, :conditions=>{:service_name=>"Business strategies"}, :page=>1, :per_page=>12, :star=>true} 

产生的记录是公司的目标,而不是办公室,这是很好的列表视图,但我想说明的相关关联办公室的谷歌地图上的图标。

找到相关的办公室记录以在半径范围内显示的最佳方式是什么?

回答

0

狮身人面像只能可靠地处理单值浮点属性 - 它没有配对的纬度/经度值的概念。这意味着您不能通过一对以上的经纬度对对象进行坚实的搜索。

最好的解决方法是真正的办公室搜索,而不是 - 也许在每个办公室的公司信息拉:

define_index do 
    indexes company.name, :as => :name, :sortable => true 
    indexes city, postal_code 

    has "RADIANS(offices.lat)", :as => :lat, :type => :float 
    has "RADIANS(offices.lng)", :as => :lng, :type => :float 
    has company.created_at, :as => :created_at 
    has company.updated_at, :as => :updated_at 
    has company_id 

    set_property :field_weights => { 
    'name'   => 10, 
    'service_name' => 9, 
    'city'   => 8 
    } 
end 

然后进行搜索时,您可以将通过COMPANY_ID,以确保只有一个结果对于任何公司(如果这是你喜欢什么):

Office.search 'foo', 
    :geo   => [lat, lng], 
    :with   => {'@geodist' => 0.0..120700.8} 
    :group_function => :attr 
    :group_by  => 'company_id' 

如果它是重要的,因为到办公室获取给定的company_id返回,那么你可能想使用:group_clause选项也是如此。文档cover this

+0

谢谢帕特,我需要也加入公司的服务,但我会尽力扭转加入:通过.... – 2011-05-30 18:37:36