【问题标题】:Getting Gmaps4Rails working with MongoMapper让 Gmaps4Rails 与 MongoMapper 一起工作
【发布时间】:2011-09-05 15:16:26
【问题描述】:

首先,这颗宝石看起来很棒——感谢@apneadiving。我希望有一天能够做出贡献——一旦我弄清楚如何正确使用它:-\

一个可怕的新手问题,我担心......而且我知道我应该能够仅基于 Ruby 主义来解决它......但我没能弄清楚我做错了什么......

我无法克服这个错误:

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

我已经探索了许多不同的编码坐标的方法,但错误——我相信——只是在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

一旦我开始工作,我将添加一篇博客文章或一个 wiki 页面以使用 MongoMapper 和 Gmaps4Rails!

【问题讨论】:

  • 完整的错误信息是什么?
  • @apneadiving 没有别的... NoMethodError (未定义的方法gmaps4rails_options' for #&lt;WaterSupply:0x00000102a76040&gt;): app/controllers/water_supplies_controller.rb:86:in map'

标签: ruby-on-rails ruby ruby-on-rails-3 mongomapper gmaps4rails


【解决方案1】:

我有一个使用 MongoMapper here 的示例

模型类如下所示:

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

【讨论】:

  • 不会从 mongo 中的位置索引值移动到 2 个纬度和经度键,从而失去能够在 mongo 中进行地理搜索的好处吗?
  • @Nick -- 我将示例更新为我目前使用的示例(我仍然需要了解更多信息)。是的,你是对的,地理索引是在 MongoDB 中利用的一个关键特性。
猜你喜欢
  • 2016-12-13
  • 2016-10-06
  • 2011-11-30
  • 2014-02-19
  • 2011-09-03
  • 2013-05-01
  • 2018-05-30
  • 2014-03-17
  • 2011-09-29
相关资源
最近更新 更多