Rails Google Maps Plugin

I still remember the awe that struck me when I first saw for the first time Google Maps in action. I was able zoom in and out, and even circumnavigate the globe by just clicking and moving the cursor around. Google Maps really demonstrated what a web 2.0 mashup could be like. Housing Maps is mashup between Google Maps and Craigslist apartment/housing ads. If you want show all the locations of Wi-Fi hot spots in your Rails app like Hotspotr, then you need to take a look at Yellow Maps 4 Ruby on Rails Plugin for Google Maps.

Before you get started you will need to sign up for a Google Maps API access. Once you have your Maps API key install the Rails plugin by executing the ‘script/plugin install <URL>’ command from your application directory where URL is replaced with the following.


The installation process will copy files in the under config, vendor/plugins, and in the public/javascripts directory. Open config/gmaps_api_key.yml in your favorite text editor and add your Google Maps API key under production for you domain. It was my experience that I did not have to change the Google Maps API key for the development and test environment. Now you are ready to start producing maps.

Update the HTML header of your template layout file so that some required JavaScript include statement are insert. Next to other javascript_include_tag statement add the following.

<%= GMap.header %>

In the controller you instantiate a GMap object with a id name. You will also need to supply the center of the map with the longitude and latitude and the zoom factor. If you want a little map balloon to appear you will need to create a marker. Your map can have any number of markers, each position separately with a title and info window when clicked. Here is the code used to construct a map, which can be placed in your controller.

@map = GMap.new("map_div_id")
@map.control_init(:large_map => true, :map_type => true)
@map.center_zoom_init([75.5,-42.56], 4)

marker = GMarker.new([75.6, -42.467],
  :title => "Where Am I?", :info_window => "Hello, Greenland")

To display your map in the action’s rhtml view file you will need to lines of code. The first line will produce JavaScript code that describes your map. The second line of code will produce the div that will display the map. If any of these two lines are omitted the map will not be displayed.

<%= @map.to_html %>
<%= @map.div(:width => 600, :height => 400) %>

These are the basics of using Google Maps in your Ruby on Rails application but YM4R can do a whole lot more. Do take the time to read the Google Maps FAQ and the YM4R GM ruby docs.

Related posts:

  1. Google Maps Builder
  2. Rails Google Analytics Plugin
  3. Updating A Rails Plugin
  4. Rails PDF Plugin
  5. Riff Rails Plugin
  6. Graphs Rails Plugin

17 Responses to “Rails Google Maps Plugin”

Leave a Reply