Module GmapHelper
In: lib/gmap_helper.rb

Generates a google-map in your rhtml

Installation

  • Put this file (gmap_helper.rb) in your RAILS_ROOT/lib/ directory
  • Include the file in your RAILS_ROOT/app/helpers/application_helper.rb

    module ApplicationHelper

     include GmapHelper
    

Including the google script in your header

 <html>
 <head>
  <%= header("12345-ABCDEF-your-api-key-from-google") %>
 </head>
 <body>

Making a map in your document

<%= gmap :option=>value, :option2=>value, .. %>

Methods

Public Instance methods

Generates a google-map in your rhtml

Usage:

<%= gmap({options}) %>

Options (all these are optional):

:mapname
set the name of the map (this is used for refering to the map with javascript, later) mapnames should be unique, if you have more than one on a page.
:width
set the width of the map, in pixels (default 400)
:height
set the height of the map, in pixels (default 400)
:point
an array containing latitute, longitude like [-114.123, 23.2563]
:marker
overlays a marker, boolean (uses :point coordinates), or an array containing a coordinate pair, defaults to false
:polyline
overlays a line, an array containing coordinate pairs [[-114.123, 23.2563], [-114.133, 23.2573]], defaults to false
:type
set the default type of map (:map or :satellite) defaults to :map
:dragging
=> :disabled turns off dragging of the map
:icon
=> :small provides a smaller style icon, ala ridefinder
:click
=> executes javascript when the map is clicked. you can access ‘pt’ which is an object with two methods, pt.x and pt.y. Example, :click=>’alert(pt.x + "," + pt.y);’
:info_window
display an information window containing text in the center of the map like :info_window=>’hello!’
:controls
display controls in the map; takes any of the following (use an array for multiples)
 :large  large controls
 :small  small controls
 :zoom   zoom in and out
 :type   select satellite/map view

example :controls=>[:small,:zoom,:type]

:white_div
a hash; if included, will show a white bar under the ‘powered by google’ stuff, so that your map looks a little cleaner. use like :white_div=>{:background_color=>’white’}
  :background_color  html-style backgroundColor, a string like 'white', 'red', or '#fff')
  :border_top        :border_top => true for a gray top border
  :height            :height=>50  for a 50px high bar. default to 25px

Examples:

 gmap(:mapname=>"my_map", :width=>"50", :height=>"120")

 gmap(:width=>"500", :height=>"500", :type=>:satellite, :point=>[-122.14944, 37.441944])

 gmap(:mapname=>"foo", :white_div=>{:background_color=>'black', :height=>'40'})

Show a link to reset a map to its initial centering <%= gmap__reset_to_center(‘your_map_name’, {:zoom=>5, :text=>’reset map!’}) renders as <a href=’#’ onclick=’(some javascript)’>reset map!</a>

Generates the script for the <head> of your html You want to include this in all your templates that will have maps in them

Usage:

 (in your .rhtml template)

 <html>
 <head>
  <%= header("12345-ABCDEF-your-api-key-from-google") %>
 </head>
 <body>

Note that you will probably have more stuff in there :)

[Validate]