I followed this tutorial to install the plug-in. I tried the second way of using Github, but it didn't work for me. I got an ym4r_gm folder in my vendor/plugins/ directory, but it was empty. So I used the third way -- manually download the plugin, unzipped the file, put them in the vender/plugins/ym4r_gm. Then copy the javascript/*.js to my public/javascript/ folder. And copy the gmaps_api_key.yml.sample to my config/ folder, renamed as gmaps_api_key.yml
After the installation is done. I didn't apply for the Google Map API key and put it in the gmaps_api_key.yml since I develop and test it in my local machine at current time.
Here is the code:
in the home_controller.rb
:
class HomeController < ApplicationController
def index
@map = GMap.new("map_div")
@map.control_init(:large_map => true,:map_type => true)
@map.center_zoom_init([75.5,-42.56],4)
@map.overlay_init(GMarker.new([75.6,-42.467],:title => "Hello", :info_window => "Info! Info!"))
end
end
in the index.html.rb. Because I separate the layout in the application.html.rb, there is only one line in this file.
<%= @map.div(:width => 600, :height => 400) %>
in the application.html.rb
<!DOCTYPE html>
<html>
<head>
<title>Housing</title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
<% unless @map == nil %>
<%= GMap.header %>
<%= @map.to_html %>
<% end%>
</head>
<body>
<%= yield %>
</body>
</html>
At the beginning, I didn't realized I separate my layout and content. So I just paste the example on my index.html.rb, but it turns out to be html code in the index.html.rb such as <html><body> becomes plain text, instead of html code. After I realized I actually separate the layout and content in application.html.rb and home.index.rb, I put the code in red lines to the application.html.rb and leave only one line in the home.index.rbAfter I have done the things above, I met another problem:
In application.html.rb, when it calls GMap.header, it should print out the Google Map information in the header such as :
<script src="http://maps.google.com/maps?file=api&v=2.x&key=ABQIA..." type="text/javascript"></script>but the '<' all becomes & lt; , I changed a bit the 'header' and 'div' method in map.rb, using html_safe when it outputs html code. Tell it not to transform '<' to & lt;
The last thing is in the 'header' method of map.rb, "ActionController::Base.relative_url_root"
is said to be a deprecated method, so it could not locate the ym4r-gm.js
So I change
#{ActionController::Base.relative_url_root}/javascripts/ym4r-gm.js
to
/javascripts/ym4r-gm.js
Here are some other useful information:
another tutorial using ym4r_gm and geokit
A tutorial in Chinese (中文)
Geokit
