You’ve got to love putting these two things together.
Google + Ruby = Awesome
Go ahead and type that into your calculator – you’ll see.
Here’s a great resource for you:
Google Maps for Ruby on Rails.
Gmaps4rails makes it easy to create Maps with Ruby on Rails 3:
- Google Maps API V3
- Directly from your model
- From your own JSON
You can even watch a screencast on this! (Link at GitHub)
Requirements are as follows:
- gem ‘gmaps4rails’ (in your gemfile, then bundle)
- <%= yield :head %> (in your header)
- <%= yield :scripts %> (in your footer)
- config.serve_static_assets = true (in your production.rb), or copy/paste gmaps4rails.css & gmaps4rails.js files in your app.
Basic config:
In your model, add:
[cc lang=”ruby”]
acts_as_gmappable
def gmaps4rails_address
self.address #describe how to retrieve the address from your model
end
[/cc]
Create a migration and add the following fields to your table (here users):
[cc lang=”ruby”]
add_column :users, :latitude, :float #you can change the name, see wiki
add_column :users, :longitude, :float #you can change the name, see wiki
add_column :users, :gmaps, :boolean #not mandatory, see wiki
[/cc]
Get it started!
In your controller:
[cc lang=”ruby”]
@json = User.all.to_gmaps4rails
[/cc]
In your view:
<%= gmaps4rails(@json) %>
Options include:
- Markers with Info window, Custom Picture
- Automatic sidebar with list of markers
- Circles, Polylines, Polygons
- Geocode directly your address and retrieve coordinates.
- Wrapper for ‘Direction’, giving instructions to go from point A to point B
- Auto-adjust the map to your markers
- Refresh your map on the fly with Javascript (and Ajax)
- More details in the Wiki
Does this look like something you could use?
Grab the files, the YouTube screencast link, and all the details from the GitHub.
JImmy says
Hi! I follow the guide, and also the guild you linked. But all that happend when try to add something is “undefined method ‘gmaps’ for #
I follow the screencast EXACTLY. any idea?