Skip to content
apneadiving edited this page Mar 19, 2011 · 54 revisions

Retrieving data

Markers are the core of the gmaps4rails gem which provides useful methods to retrieve all data from the model bearing acts_as_gmappable.

Say your User model contains acts_as_gmappable, then:

@markers = User.all.to_gmaps4rails    #@markers contain valid json to pass to the view

Or:

@markers = User.your_scope.to_gmaps4rails    #@markers contain valid json to pass to the view

Customize what is displayed

Info windows

To add an infowindow (visible when you click a marker), add this method in your model:

    def gmaps4rails_infowindow
      # add here whatever html content you desire, it will be displayed when users clicks on the marker
    end

The infowindow can contain whatever you want. Example:

    def gmaps4rails_infowindow
      "<img src=\"#{self.picture}\"> #{self.name}"
    end

Add a title to your marker

To add a title (visible when you are over a marker), add this method in your model:

    def gmaps4rails_title
      # add here whatever text you desire
    end

Customize each marker

Include this method in your model to customize markers:

def gmaps4rails_marker_picture
 {
  "picture" => "",
  "width" => "",
  "height" => ""
 }
end

Note: the picture should be properly resized.

Example:

def gmaps4rails_marker_picture
  {
  "picture" => "http://www.blankdots.com/img/github-32x32.png",
  "width" => "32",
  "height" => "32"
  }
end        

Creating your own json

Format

If you want to use your own json to initialize the map, create your json with the following attributes

    @markers = '[
             {"description": "", "title": "", "longitude": "", "latitude": "", "picture": "", "width": "", "height": ""},
             {"longitude": "", "latitude": "" }
            ]'

Only latitude and longitude are mandatory.

You can customize any single marker with the other attributes:

  • picture, width and height are related to the marker

  • description could contain whatever html you want to be displayed in the infowindow.

Displaying your markers

Standard

In your view:

<%= gmaps("markers" => { "data" => @markers }) %>

Shortcut

In your view:

<%= gmaps4rails(@markers) %>

This displays the markers with auto-adjust option set to true. No other option could be passed this way.

Options

Options for markers are:

  • marker_picture

  • marker_width

  • marker_length

  • do_clustering, to cluster makers, default to true

  • clusterer_gridSize, the more the quicker but the less precise, default to 50

  • clusterer_maxZoom, define at which zoom level clusterer is disabled, default to 10

  • randomize: false, Google maps can't display two markers which have the same coordinates. This randomizer enables to prevent this situation to happen.

  • max_random_distance: 100 in meters. Each marker coordinate could be altered by this distance in a random direction

These options set default styling for your markers.

You can pass options this way:

<%= gmaps("markers" => { "data" => @markers, "options" => { "marker_picture" => your_pic, "marker_width" => 22, "marker_length" => 32 } }) %>

Clone this wiki locally