Google Maps API in Rails (YM4R-GM) – Part II: Usage

June 27, 2009

Environment: Rails 2.3.2, Windows Vista, SQLite3 development on a local box

Using the YM4R is pretty simple. Here’s what you need to do:

1 – In your controller you need to add the following lines:

@map = GMap.new("map_div")
@map.control_init(:large_map => true,:map_type => true)
@map.set_map_type_init(GMapType::G_HYBRID_MAP)
@map.center_zoom_init([22,38],4)
@map.overlay_init(GMarker.new([22,38],:title =&gt; "Title", :info_window =&gt; "This description<br/> can have <b>HTML</b> in it!"))

@map.center_zoom_init([22,38],4) – first parameter is latitude, second is longitude (it usually comes with a decimal point and can be negative, eg. -12.3456), third is the zoom level (0 – world view, 19 – highest possible).

@map.overlay_init is how you set up the markers. You can change a marker icon if you want, you can also put html (including links) in the :info_window. Info window is what you see when you click on the marker.

@map.set_map_type_init(GMapType::G_HYBRID_MAP) By default you can view the map in the NORMAL mode. If you want to run in HYBRID, you need to add this line.

2 – Now you need to modify your layout to load the needed Javascript libraries. The layout is in views/layouts directory. Open the one for your controller and add this line just below your generic javascript loading line:

<%= GMap.header %>

3 – In your action view add the following 2 lines to render the map:

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

A note on @map.to_html – some tutorials would suggest you add it to your layout, I think it’s not the correct way, because layout exists for the whole controller and you will usually want to define the @map only in particular actions (like show). So in all other cases you will get a mistake of an undefined variable.

That’s it. If you need additional instruction, you can get a lot from reading the extended tutorial on its Git page.

Entry Filed under: Uncategorized. .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent posts

Starting to learn Rails?

Archive

Recent comments

Ryan H on Installing Rails on Windows (3…
gogetakame on Installing Rails on Windows (3…
gogetakame on Installing Rails on Windows (3…
allaboutruby on Installing Rails on Windows (3…
Harsha on Installing Rails on Windows (3…