简体   繁体   中英

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. If this cannot be done with the gem, is there another resource that will allow me to geocode two locations on the same form? 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 .

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. The first one is easier, and solves the problem. The second one is cooler (might play with that myself as a 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.

I think you're going to have to manually geocode, using the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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