<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>redirect_to :hack &#187; General</title>
	<atom:link href="http://blog.coderubik.com/category/general/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.coderubik.com</link>
	<description>Thoughts on Ruby on Rails and Web development</description>
	<lastBuildDate>Thu, 25 Mar 2010 17:19:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Display a Google Map with Rails</title>
		<link>http://blog.coderubik.com/2009/10/display-a-google-map-in-rails/</link>
		<comments>http://blog.coderubik.com/2009/10/display-a-google-map-in-rails/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 13:07:28 +0000</pubDate>
		<dc:creator>Pierre Olivier Martel</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://modelix.net/?p=137</guid>
		<description><![CDATA[In my last article on geopositioning with Rails and Google maps, we saw how to use the YM4R/GM plugin to find and store the longitude/latitude of an address using the Google Maps api. With these informations, we can now display the map to the user. We&#8217;ll see how to do this in three easy steps. [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="/2009/10/rails-geopositioning-with-google-maps/">my last article on geopositioning with Rails and Google maps</a>, we saw how to use the <a href="http://ym4r.rubyforge.org/"><span class="caps">YM4R</span>/GM plugin</a> to find and store the longitude/latitude of an address using the Google Maps api. With these informations, we can now display the map to the user. We&#8217;ll see how to do this in three easy steps.</p>

<h2>1. Prepare the map in your model</h2>

<p>In your <code>Geolocation</code> model, start by adding this snippet of code which will initialize your map.</p>



<pre class="prettyprint rb"><code>def map
  @map ||= prepare_map
end

def mapped?
  latitude &amp;&amp; longitude
end

def coordinates
  [latitude, longitude]
end

private

def prepare_map
  return unless mapped?
  returning GMap.new(&quot;gmap&quot;) do |map| 
    map.control_init(:large_map =&gt; true, :map_type =&gt; true)
    map.center_zoom_init(coordinates, 15) # Set the center and zoo level
    map.overlay_init(GMarker.new(coordinates))
  end
end
</code></pre>



<p>The important method here is <code>prepare_map</code>. What it does is create a new map centered at the given coordinates. The <code>overlay_init</code> method sets a marker at the coordinates position. This is a basic vanilla map initialization. You might want to read the <a href="http://ym4r.rubyforge.org/ym4r_gm-doc/"><span class="caps">YM4R </span>api</a> for more options like adding an info window or multiple markers.</p>

<h2>2. Call it from your controller</h2>

<p>In our controller, we will set the <code>@map</code> variable to make it accessible in our view :</p>



<pre class="prettyprint rb"><code>@map = customer.geolocation.map</code></pre>



<h2>3. Display the map in your view</h2>

<p>You will need this snippet of code in the <code>header</code> section of your document to do the proper javascript imports and initializations :</p>



<pre class="prettyprint rb"><code>= GMap.header
= @map.to_html
</code></pre>



<p>And in the body of your view, where you want to display the map, simply add :</p>



<pre class="prettyprint rb"><code>@map.div(:width =&gt; 600, :height =&gt; 400)</code></pre>



<p>This last line will generate a div with the id &#8220;gmap&#8221; (or whatever name you set in the <code>GMap.new</code> function). I find it&#8217;s cleaner to just set the width an height through <span class="caps">CSS.</span></p>

<h2>4. Adding complexity</h2>

<p>This technique will only work if you display only one map on the page. The map needs to be visible on page load to be properly displayed. But what if there is multiple maps you want to display on the same page? And what if you want to show them in overlay popups? You probably don&#8217;t want to initialize them all on page load since it will freeze the client browser. This will require some javscript hacking. I will show you how to do that next week!</p>]]></content:encoded>
			<wfw:commentRss>http://blog.coderubik.com/2009/10/display-a-google-map-in-rails/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->