Rails Google Maps Plugin
Feb15
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.
svn://rubyforge.org/var/svn/ym4r/Plugins/GM/trunk/ym4r_gm
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.
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.
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.
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.
Technorati Tags: plugin, plugins, rails, rubyonrails, gmaps, google maps, google, api, ruby, ym4r, gm, ym4r4gm, marker, hotspotr, craigslist











5:57 am on February 8th, 2007
Thanks for the overview - looks very useful way to get gmaps started quickly.
12:13 pm on February 12th, 2007
@Dr Nic - Thanks for your comment. YM4R Google Maps Plugin has a funny name, but it seems to me that it also has a pretty solid library for working with Google Maps in a Ruby on Rails application.
12:05 am on March 17th, 2007
Great information.
I’m reasonably new to rails and google maps plugins, however there appears to be quite a few rails map plugins available (e.g. YM4R, GeoKit, Cartographer). Do you have any experience and comparisons with the other map alternative plugins? Thanks
7:48 pm on March 19th, 2007
@Bob - Thanks for your comments. As to your question, I haven’t played with GeoKit as of yet but from what I gather it is a geocoding plugin, not necessarily a map plugin. From what I could tell it won’t display a map, but given an address it will give you the longitude/latitude, giver a zip code it will find you the center of that location, etc. Cartographer is more like YM4R in that it is a plugin that will display a map. I did look into Cartographer but I remember choosing YM4R over it because the people behind YM4R seemed active in mapping (they also provide a goecoding toolkit for Ruby) and they support Yahoo Maps.
I hope this was helpful!
11:37 pm on March 20th, 2007
For a comparison of rails geo-plugins have a look at this post from James Stewart: Comparing Rails Geo Plugins
10:22 am on March 21st, 2007
@Bob - I forwarded your question to Andre Lewis, the author of GeoKit and the new book Beginning Google Maps Applications with Rails and Ajax. This is what he said…
2:19 am on September 25th, 2007
Hi,
I wrote a geocoding tool recently - it’s easy to use and supports Google, Yahoo and MapQuest. It doesn’t have bells and whistles but it does geocoding pretty well! Hope it helps someone: GeoX Rails Geocoding Plugin
9:32 am on October 29th, 2007
The ruby script/plugin install svn://rubyforge.org/var/svn/ym4r/Plugins/GM/trunk/ym4r_gm
Doesn’t work for me. Do I have to install something special to be able to use SVN?
9:54 am on October 29th, 2007
@Patrick - Install the SVN client for you OS.
12:07 am on January 12th, 2008
How can a user add a new marker like they do on hotspotr?
12:35 am on November 14th, 2008
Thanks for introducing this to me, been working with it all day, and got GeoKit in the mix so i can convert addresses to lat/lng. Since only the Gmarker takes addresses but the center_zoom_init does not. It gets funky and puts me in like Greenland or Russia.. Which I have no problem with, just not what I desired as an outcome :D