Nishtha
Nishtha

Reputation: 433

How to get latitude and longitude

I am trying to give input address or a place and I need to fetch latitude and longitude of that place. I am trying to use geocoder gem but it is not working. I have an address field in my users table but I don't have latitude and longitude field in my users table. I am providing an address and by that address I need to fetch latitude and longitude. What am I doing wrong?

class User < ActiveRecord::Base
  geocoded_by :address

 def self.find_cordinates(users)
        puts "Ssss{User.near(users.last['address'],5000).to_json}"

end


    end

Upvotes: 0

Views: 732

Answers (1)

Michael Malov
Michael Malov

Reputation: 1887

You need to add latitude and longitude fields to your model

rails generate migration AddLatitudeAndLongitudeToUser latitude:float longitude:float
rake db:migrate

Than add this to User model

geocoded_by :address
after_validation :geocode

Run

rake geocode:all CLASS=User    

If you don't want to change your model you can use utility method to get coordinates by address

Geocoder.coordinates("25 Main St, Cooperstown, NY")

Upvotes: 2

Related Questions