2011-09-05 24 views
2

首先,這個gem看起來很棒 - 感謝@apneadiving。我希望能夠有一天能夠貢獻 - 一旦我弄清楚如何正確使用它: - \獲取Gmaps4Rails使用MongoMapper

一個可怕的新手問題,我擔心......並且我知道我應該能夠單獨根據它關於Ruby-主義......但我沒能找出我做錯了......

我不能讓過去的這個錯誤:

NoMethodError (undefined method `gmaps4rails_options' for <WaterSupply>... 

我已經探討了很多不同的方式來編碼COORDS ,但是我相信這個錯誤只是在acts_as_gmappable中,而不是「工作」。我的模型是這樣的:

class WaterSupply 
    include Gmaps4rails::ActsAsGmappable 
    include MongoMapper::Document 

    acts_as_gmappable :process_geocoding => false 

    ensure_index [[:loc, '2d']] 

    def initialize 
    puts Gmaps4rails::ActsAsGmappable.inspect 
    puts "*"*50 
    end 

    key :name, String, :required => true 

    # TODO break this address/geo stuff out into a separate Location class 
    key :loc, GeoPoint, :default => [40.34962381,-74.75102367] 
    key :gmaps, Boolean 
    key :address, String 
    key :city, String 
    key :zip, String 
    key :country, String 

    def gmaps4rails_address 
     "#{self.address}, #{self.zip} #{self.city}, #{self.country}" 
    end 
end 

任何幫助,將不勝感激。我可以得到一個空白的地圖出現,只是沒有任何模型實例數據:-p

一旦我的東西能夠正常工作,我會添加一個博客文章或一個使用MongoMapper和Gmaps4Rails的wiki頁面!

+0

什麼是完整的錯誤消息? – apneadiving

+0

@apneadiving沒有多少人... NoMethodError(未定義的方法'gmaps4rails_options'爲#<給水:0x00000102a76040>): 應用程序/控制器/ water_supplies_controller.rb:86:在'映射」 –

回答

2

我有一個例子與MongoMapper工作here

Model類看起來是這樣的:

class WaterSupply 
    include MongoMapper::Document 
    include Gmaps4rails::ActsAsGmappable 
    acts_as_gmappable :lat => 'latitude', :lon => 'longitude', :process_geocoding => true, 
        :check_process => :prevent_geocoding, 
        :address => "address", :normalized_address => "address" 
        #:msg => "Sorry, not even Google could figure out where that is" 

    key :name, String 
    key :address, String 
    key :street, String 
    key :zip, String 
    key :city, String 
    key :state, String 
    key :country, String 
    key :latitude, Float 
    key :longitude, Float 
    key :gps, GeoPoint # lat, lon; e.g., [40.34962381,-74.75102367] 
    key :gmaps, Boolean 

    ensure_index [[:gps, "2d"]] 

    before_save :store_geo 

    def store_geo 
    self.gps = [self.latitude, self.longitude] 
    end 

    def prevent_geocoding 
    address.blank? || (!latitude.blank? && !longitude.blank?) 
    end 
    def gmaps4rails_address 
    "#{self.street}, #{self.city}, #{self.state} #{self.zip} #{self.country}" 
    end 
    #def gmaps4rails_infowindow 
    # "#{self.name} #{self.gps}" 
    #end 
    def gmaps4rails_title 
    "#{self.name}" 
    end 

    def gmaps4rails_sidebar 
    "#{self.name} #{self.gps}" 
    end 

end 
+0

不從一個位置移動mongo中的索引值爲2個經緯度鍵,可以在mongo中進行geo搜索嗎? – Nick

+0

@Nick - 我更新了我目前使用的示例(我仍然需要了解更多信息)。是的,你是對的,地理索引是在MongoDB中利用的一個關鍵特性。 –