Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:34:14+00:00 2026-06-14T12:34:14+00:00

I am trying to use the geocoder gem for looking up addresses and coordinates.

  • 0

I am trying to use the geocoder gem for looking up addresses and coordinates. I want it to work together with my PostGIS spatial database and the RGeo gem, which uses POINT features instead of saving latitude and longitude values separately. So I have tried to work into my model to save the results from the geocoder lookup into an RGeo POINT feature:

class Location < ActiveRecord::Base
  attr_accessible :latlon, :name, :address
  set_rgeo_factory_for_column(:latlon, RGeo::Geographic.spherical_factory(:srid => 4326))

  geocoded_by :name_and_address do |obj, results|
    if geo = results.first
      obj.latlon = Location.rgeo_factory_for_column(:latlon).point(geo.longitude, geo.latitude)
    end
  end

  after_initialize :init
  after_validation :geocode


  def init
    self.latlon ||= Location.rgeo_factory_for_column(:latlon).point(0, 0)
  end

  def latitude
    self.latlon.lat
  end

  def latitude=(value)
    lon = self.latlon.lon
    self.latlon = Location.rgeo_factory_for_column(:latlon).point(lon, value)
  end

  def longitude
    self.latlon.lon
  end

  def longitude=(value)
    lat = self.latlon.lat
    self.latlon = Location.rgeo_factory_for_column(:latlon).point(value, lat)
  end

  def name_and_address
    "#{self.name}, #{self.address}"
  end

end

In the Rails console I can now do:

test = Location.new(name: "Eiffel Tower") // => #<Location id: nil, name: "Eiffel Tower", latlon: #<RGeo::Geographic::SphericalPointImpl:0x81579974 "POINT (0.0 0.0)">, created_at: nil, updated_at: nil, address: nil>
test.geocode    //  => #<RGeo::Geographic::SphericalPointImpl:0x81581ac0 "POINT (2.294254 48.858278)">

Wonderful!
However, when I want to save my test Location, which triggers the :after_validation call for :geocode I get an error:

NoMethodError: undefined method `lon' for nil:NilClass
    from /Users/sg/rails-projects/geo_rails_test/app/models/location.rb:24:in `latitude='
    from /Users/sg/rails-projects/geo_rails_test/app/models/location.rb:7:in `block in <class:Location>'
    from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/geocoder-1.1.2/lib/geocoder/stores/base.rb:108:in `call'
    from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/geocoder-1.1.2/lib/geocoder/stores/base.rb:108:in `do_lookup'
    from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/geocoder-1.1.2/lib/geocoder/stores/active_record.rb:278:in `geocode'
    from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `_run__1498365873506454431__validation__51840359655181017__callbacks'
    [...]

It seems that the geocode callback deletes the RGeo object so that its built in methods as well as my getter and setter methods are no longer available.
Why? Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:34:15+00:00Added an answer on June 14, 2026 at 12:34 pm

    It was a bloody typo…

    It works. I leave it up, maybe someone else has the same issue or a better solution…

    I have also added the reverse_geocode case.

    Now look what it can do:

    1.9.3p194 :310 > loc = Location.new(name: "Vatikan")
     => #<Location id: nil, name: "Vatikan", latlon: #<RGeo::Geographic::SphericalPointImpl:0x8148add8 "POINT (0.0 0.0)">, created_at: nil, updated_at: nil, address: nil> 
    1.9.3p194 :311 > loc.save
       (0.2ms)  BEGIN
      SQL (0.6ms)  INSERT INTO "locations" ("address", "created_at", "latlon", "name", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["address", "Via Pio X, 00120, Vatican City"], ["created_at", Sat, 17 Nov 2012 19:26:59 UTC +00:00], ["latlon", #<RGeo::Geographic::SphericalPointImpl:0x81570554 "POINT (12.453389 41.902916)">], ["name", "Vatikan"], ["updated_at", Sat, 17 Nov 2012 19:26:59 UTC +00:00]]
       (0.9ms)  COMMIT
     => true 
    

    I love this! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Google geocoder service to get the coordinates of cities input
I'm trying to use the Google Maps geocoder with Python and JSON, but keep
I am trying use gem tire to search in my application. I have tables
I'm trying to use the Google Geocoder API to get me a Latitude and
I am trying use Date Picker and on Select i want to display the
I am using geocoder and the devise gem. And i am trying to get
Im trying to use geocoder for displaying a location based on an address. My
I'm trying to use the geocoder to get co-ordinates for the following address: 1945
I am trying to get this: http://code.google.com/p/geocoder-java/ into eclipse to work with the geocoder
I am trying to use the google geocoder api to get the longitude and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.