简体   繁体   English

Rails geocoder gem问题

[英]Rails geocoder gem issues

I am using the geocoder gem and I need to code a start and end address for a model, however I am not sure if I can accomplish this using this gem and if I can whether I am doing it correctly. 我正在使用地理编码器gem,我需要为模型编写开始和结束地址,但是我不确定是否可以使用此gem完成此操作,如果我能够正确执行此操作。 If this cannot be done with the gem, is there another resource that will allow me to geocode two locations on the same form? 如果使用gem无法做到这一点,是否有另一种资源可以让我在同一表单上对两个位置进行地理编码? This is an example of what I am trying to do, but it just passes the second address to be geocoded. 这是我想要做的一个例子,但它只是通过第二个地址进行地理编码。

geocoded_by :start_address
before_validation :geocode
geocoded_by :delivery_address, :latitude => :end_latitude, :longitude => :end_longitude
before_validation :geocode

If you look at the source it looks as though there's an options hash that would get overwritten active_record.rb and base.rb . 如果你看一下它看起来好像有一个选项哈希会被覆盖的active_record.rbbase.rb。

I figure there are two options: move your addresses into an included (joined) model (like Address or something), or fork geocoder to have multiple options by key. 我认为有两种选择:将您的地址移动到包含(加入)模型(如Address或其他东西),或者fork地址解析器按键有多个选项。 The first one is easier, and solves the problem. 第一个更容易,并解决了问题。 The second one is cooler (might play with that myself as a kata). 第二个更酷(可能与我自己一起玩kata)。

So what's going on is just simple ruby... if you do this: 所以发生的事情只是简单的红宝石......如果你这样做:

class Question
  def ask
    "what would you like?"
  end

  def ask
    "oh hai"
  end
end

Question.new.ask
 => "oh hai"

The last method definition wins... so when you declare two geocoded_by methods, the 2nd is the one that counts. 最后一个方法定义获胜...所以当你声明两个geocoded_by方法时,第二个是重要的方法。

I think you're going to have to manually geocode, using the gem 我认为你将不得不使用gem进行手动地理编码

before_validation :custom_geocoding

def custom_geocoding
  start_result = Geocoder.search(start_address)
  end_result = Geocoder.search(delivery_address)
  # manually assign lat/lng for each result
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM