【问题标题】:How to setup Geocoder to use database for lat/long如何设置地理编码器以使用数据库进行纬度/经度
【发布时间】:2013-11-26 05:55:37
【问题描述】:

我安装了 Geocoder gem,我想禁用 API。我在邮政编码表中有 47,000 个纬度/经度坐标。我想使用数据库中的数据,而不是 Google 的 API。

我将如何做到这一点,以便它调用我数据库中的邮政编码表并找到最近的纬度/经度。

用户模型:

class User < ActiveRecord::Base
  has_secure_password
  attr_accessible :role, :age, :age_end, :password_confirmation, :about_me, :feet, :inches, :password, :birthday, :career, :children, :education, :email, :ethnicity, :gender, :height, :name, :password_digest, :politics, :religion, :sexuality, :user_drink, :user_smoke, :username, :zip_code
  has_many :users, dependent: :destroy
  validates_format_of :zip_code,
                  with: /\A\d{5}-\d{4}|\A\d{5}\z/,
                  message: "should be 12345 or 12345-1234"
  validates_uniqueness_of :email
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
  validates_presence_of :password, :on => :create
  before_create { generate_token(:auth_token) }
  ROLES = %w[admin user guest banned]

  # models/user.rb
  after_create :setup_gallery


   def over_18
      if birthday + 18.years > Date.today
        errors.add(:birthday, "can't be under 18")
      end
    end

    def age
      now = Time.now.utc.to_date
      now.year - birthday.year - ((now.month > birthday.month || (now.month == birthday.month && now.day >= birthday.day)) ? 0 : 1)
    end

  def to_s; username
  end

  def has_role?(role_name)
    role.present? && role.to_sym == role_name.to_sym
  end

  def send_password_reset
    generate_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
  end
  end

【问题讨论】:

    标签: ruby-on-rails rails-geocoder


    【解决方案1】:

    在您的User.rb 中,执行此操作

    def geocode
      [self.zip_code.latitude, self.zip_code.longitude]
    end
    

    现在运行User.near(my_user.geocode)

    如果这有帮助,请告诉我。

    【讨论】:

    • 这个...根本没有回答问题。
    • 如果您参与了我们的私人电子邮件链,也许您不会这么说。
    • 也许我不会,但可惜的是,我不是而且只能按照我在这里看到的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    相关资源
    最近更新 更多