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.

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.

Enjoy. Share. Be Happy.
  • Twitter
  • Facebook
  • StumbleUpon
  • del.icio.us
  • Tumblr
  • Google Bookmarks
  • FriendFeed
  • Yahoo! Buzz
  • Reddit
  • Digg
  • HackerNews
  • Suggest to Techmeme via Twitter
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • Mixx
  • Furl

Related posts:

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

This entry was posted in JavaScript, Ruby, TechKnow. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

13 Comments

  1. Posted February 8, 2007 at 5:57 am | Permalink

    Thanks for the overview – looks very useful way to get gmaps started quickly.

  2. Posted February 12, 2007 at 12:13 pm | Permalink

    @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.

  3. Bob
    Posted March 17, 2007 at 12:05 am | Permalink

    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

  4. Posted March 19, 2007 at 7:48 pm | Permalink

    @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!

  5. Posted March 20, 2007 at 11:37 pm | Permalink

    For a comparison of rails geo-plugins have a look at this post from James Stewart: Comparing Rails Geo Plugins

  6. Posted March 21, 2007 at 10:22 am | Permalink

    @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…

    YM4R provides view helpers for rendering maps on your views without having to worry about JavaScript. Cartographer does the same, although last time I checked it was lagging far behind YM4R in terms of functionality and completeness.

    GeoKit on the other hand focuses on a robust and flexible geocoding system, and extending ActiveRecord with easy-to-use distance finders ( Store.find :all, :origin=>’94102′, :within=>10).

    There is some overlap between YM4R and GeoKit, but for the most part their functionality is complementary to one another. For example, you can use GeoKit for its AR extensions and IP-based geolocation, and YM4R to render the map on your views. You can also use GeoKit for its AR extensions and use JavaScript to render the map. The latter is the approach I use, since it affords the most flexibility, and keeps me up-to-date with new features in the Google Maps API.

  7. Posted September 25, 2007 at 2:19 am | Permalink

    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

  8. Patrick
    Posted October 29, 2007 at 9:32 am | Permalink

    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. Posted October 29, 2007 at 9:54 am | Permalink

    @Patrick – Install the SVN client for you OS.

  10. yourtube
    Posted January 12, 2008 at 12:07 am | Permalink

    How can a user add a new marker like they do on hotspotr?

  11. Posted November 14, 2008 at 12:35 am | Permalink

    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

  12. Posted September 4, 2009 at 2:50 am | Permalink

    Thanks, took me only a couple of minutes to enable Google Maps for my rails application. :)

  13. Posted November 20, 2009 at 4:59 am | Permalink

    Thanks for this post.
    It’s very clear and helped me in using this plugin the first time.

4 Trackbacks

  1. [...] 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 [...]

  2. By A Fresh Cup » Blog Archive » Quick Links on January 3, 2008 at 7:49 am

    [...] Rails Google Maps Plugin – The basics to using Yellow Maps 4 Ruby . (via dzone) [...]

  3. [...] Juixe TechKnow ยป Rails Google Maps Plugin – [...]

  4. By Rails Google Maps Plugin » My Web Stuff on November 21, 2008 at 5:56 pm

    [...] http://www.juixe.com/techknow/index.php/2007/02/07/rails-google-maps-plugin/ [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*