Search code examples
ruby-on-railsruby-on-rails-3reverse-geocodingrails-geocoder

Ruby Geocoder multiple reverse geocoders


I need to reverse geocode two locations "from" and "to".

Using the geocoder gem (http://www.rubygeocoder.com/) I've tried:

  reverse_geocoded_by :from_lat, :from_long, :address => :from_string      
  reverse_geocoded_by :to_lat, :to_long, :address => :to_string   
  after_validation :reverse_geocode  

but only the second (to_string) is successfully reverse coded.

Can the geocoder gem support more than one geocoding step?


Solution

  • I banged my head against the wall for a while on this one too.

    Try creating your own def:

    after_validation :reverse_geocode_both
    

    ...

    def reverse_geocode_both
        start_coordinates = [self.from_lat, self.from_long]
        end_coordinates = [self.to_lat, self.to_long]
        self.from_string = Geocoder.address(start_coordinates)
        self.to_string = Geocoder.address(end_coordinates)
    end