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.
<%= 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")
@map.overlay_init(marker)
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:
13 Comments
Thanks for the overview – looks very useful way to get gmaps started quickly.
@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.
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
@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!
For a comparison of rails geo-plugins have a look at this post from James Stewart: Comparing Rails Geo Plugins
@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…
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
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?
@Patrick – Install the SVN client for you OS.
How can a user add a new marker like they do on hotspotr?
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
Thanks, took me only a couple of minutes to enable Google Maps for my rails application. :)
Thanks for this post.
It’s very clear and helped me in using this plugin the first time.
4 Trackbacks
[...] as there are already plenty of posts about this out there. Check out Tom Taylor, Andre Lewis or Juxie’s posts just to name a few. This post is solely to help others avoid the problems I have [...]
[...] Rails Google Maps Plugin – The basics to using Yellow Maps 4 Ruby . (via dzone) [...]
[...] Juixe TechKnow ยป Rails Google Maps Plugin – [...]
[...] http://www.juixe.com/techknow/index.php/2007/02/07/rails-google-maps-plugin/ [...]