Repository navigation
Markers
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
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
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
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
in your model
def gmaps4rails_sidebar
"<span class="foo">#{name}</span>" #put whatever you want here
end
In your view, you must add a <ul>
<ul id="markers_list"> </ul>
and call gmaps4rails properly:
<%= gmaps("markers" => {"data" => @json, "options" => {"list_container" => "markers_list" } }) %>
If you want to use your own json to initialize the map, create your json with the following attributes
@markers = '[
{"description": "", "title": "", "sidebar": "", "longitude": "", "latitude": "", "picture": "", "width": "", "height": ""},
{"longitude": "", "latitude": "" }
]'
Only latitude and longitude are mandatory.
You can customize any single marker with the other attributes:
-
picture,widthandheightare related to the marker -
descriptioncould contain whatever html you want to be displayed in the infowindow. -
sidebarcontains the data to be displayed in the list of marker
In your view:
<%= gmaps("markers" => { "data" => @markers }) %>
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 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 from happening. -
max_random_distance: 100in meters. Each marker coordinate could be altered by this distance in a random direction -
list_container: null, if a string is passed, it will be assumed that it's the id of the<ul>in which you want the list of markers to be included. Definegmaps4rails_sidebarmethod in your model to customize what you want to be displayed in each<li> -
anchor: null, Anchor position of the marker image. Default is null <=> bottom_center, you can set options: top_left, top_center, top_right, center_left, center, center_right, bottom_right, bottom_center, bottom_left -
custom_clusterer_pictures: see help below
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 } }) %>
You can customize the pics used by the clusterer by setting the gmaps_custom_clusterer_pic js function in your code.
Here is an example:
function gmaps_custom_clusterer_pic() {
var url = "http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/1.0";
return [{
url: url + '/images/heart30.png',
height: 26,
width: 30,
},
{
url: url + '/images/heart40.png',
height: 35,
width: 40,
},
{
url: url + '/images/heart50.png',
width: 50,
height: 44,
}];
}