Enables easy display of items (taken from a Rails 3 model) with Google Map, OpenLayers, Bing or Mapquest. Geocoding + Directions included. Provides much options: markers customization, infowindows, auto-adjusted zoom, polylines, polygons, circles etc... See wiki for full description and examples.
Pull request Compare This branch is 6 commits ahead, 234 commits behind apneadiving:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Google Maps for Rails (gmaps4rails) Build Status

Gmaps4rails is developed to simply create a Google Map:

  • directly from your model,

  • from your own json

It's based on Ruby on Rails 3 Engines and uses Google Maps API V3.

See (somewhat outdated) screencasts here: www.youtube.com/user/TheApneadiving

When Gmaps4rails finally means Global Maps for Rails

I've added support for other map providers: openlayers, mapquest & bing.

See: github.com/apneadiving/Google-Maps-for-Rails/wiki/Map-Apis

Any help would be appreciated to complete this work.


1) Gemfile

gem 'gmaps4rails'

2) Copy the assets to your app (and be sure to copy the fresh assets when you upgrade the gem):

rails generate gmaps4rails:install

This will copy all the coffeescript files if you're running Rails >= 3.1 or all the js files for Rails 3.0.x.

Only `gmaps4rails.base` + `gmaps4rails.your_provider` are necessary.

3) Javascript

<%= yield :scripts %> (in your footer)

This container will include the js files from map providers, the custom js to display the map, the gem's js code.

4) CSS

`gmaps4rails.css` will be copied to your app after you run the Rails generator. Be sure to require this file in your view for your first steps.

For Rails 3.0.x or without assets pipeline:

<%= stylesheet_link_tag 'gmaps4rails' %>

For Rails >= 3.1 with assets pipeline, same principle: add the required file to your manifest.

Basic configuration

In your model, add:


def gmaps4rails_address
#describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
  "#{self.street}, #{self.city}, #{self.country}" 

Create a migration and add the following fields to your table (here users):

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

Basic configuration: Non-relational DB

Mongoid example:

acts_as_gmappable :position => :location

field :location, :type => Array

By default this uses the [lng, lat] position format. You can reverse this format, by specifying `:pos_order => [:lng, :lat]`

From [geocoder](github.com/alexreisner/geocoder) - Latitude/Longitude Order.

_“Coordinates are generally printed and spoken as latitude, then longitude ([lat,lon]).Geocoder respects this convention and always expects method arguments to be given in [lat,lon] order. However, MongoDB requires that coordinates be stored in [lon,lat] order as per the GeoJSON spec (geojson.org/geojson-spec.html#positions), so internally they are stored “backwards.” However, this does not affect order of arguments to methods when using Mongoid or MongoMapper.”_

How to?


In your controller:

@json = User.all.to_gmaps4rails

In your view:

<%= gmaps4rails(@json) %>



  • Markers with Info window, Custom Picture, RichMarkers (make your own markers with custom html)

  • 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)

  • KML support

  • Easy multimap

  • More details in the Wiki


Feel free to contact us, you have your say.

Want to help?

This is how you can launch the spec suite (js + ruby):

  • Clone the gem repository

  • Go to the gem's folder

  • run `bundle`

  • run `cd spec/dummy; rake db:migrate RAILS_ENV=test; cd ../..`

  • run `bundle exec rspec spec`


MIT license.

Authors: Benjamin Roth, David Ruyer