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

June 27, 2009 at 10:01 am Leave a comment

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.

About these ads

Entry filed under: Uncategorized. Tags: .

Google Maps API in Rails (YM4R-GM) – Part I: Installing the beast Split a string in two (or three)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent posts

Starting to learn Rails?

Kindle

Get Kindle - the best e-book reader, that I personally use, and the only one that you can read on the beach - very useful: Kindle Wireless Reading Device (6" Display, Global Wireless, Latest Generation)

Follow

Get every new post delivered to your Inbox.

Join 30 other followers

%d bloggers like this: