1

I have my own database that includes: address, lat, long, name. I have all the addresses that I want without their coordinates. Is there a way to find the coordinates in order to use them as markers on my active map?

In other words I want something to batch?! geocode my addresses that are already in to my database.

Thank you very much

Edited:

def index
if params[:search].present?
  @locations = Location.near(params[:search], 20, :order => :distance)
else
  @locations = Location.all
  @locations.each do |l|
    if l.latitude.nil?
      new_location = "#{l.address}"
      s = Geocoder.search(new_location)
      l.latitude = s[0].latitude
      l.longitude = s[0].longitude
      l.save
    end
  end
end

This is what I have, but it updates only my first address on the database.

j0k
  • 22,600
  • 28
  • 79
  • 90
ThinkFast
  • 23
  • 6

1 Answers1

0

Check out the gmaps4rails gem at https://github.com/apneadiving/Google-Maps-for-Rails

Here's what I use in the model one of my apps:

 acts_as_gmappable lat: 'latitude', lng: 'longitude', 
   process_geocoding: :geocode?,
   address: :full_address, 
   msg: "Sorry, not even Google could figure out where that is"

Oh, and here's my geocode? method:

def geocode?
  latitude.blank? || longitude.blank?
end

So when the model's latitude and/or longitude are nil, gmaps4rails automatically does a lookup and fills in the entries.

Donovan
  • 15,917
  • 4
  • 22
  • 34
  • Hi Donovan, this is what i use plus geocoder. My problem is that when i use a new database that has already addresses, i want to update their coordinates. – ThinkFast Dec 17 '13 at 21:30
  • That gem already has provisions for that, but you'll have to run a manual task to rebuild the coordinates. In my app, I simply set longitude and latitude to nil and gmaps4rails rebuilds the location. – Donovan Dec 17 '13 at 21:32
  • Thank you for your help so far. I know that will work when you create a new address through your index. But how is this going to work when you have a database with 300 addresses already without the coordinates? – ThinkFast Dec 17 '13 at 21:42
  • If it's a one-time-deal, just run a quick `rails console` command to update the records in question. When you save them, `acts_as_gmappable` should kick in and do it's thing. If it happens often, create a rake task. – Donovan Dec 17 '13 at 21:45