Make Gmaps4Rails and Grid-a-Licious Work Together

Cover image

For my Coding Da Vinci Project "Wiederaufbau Ost-Berlin" we decided to show a list of building-names and link them to markers on Google Maps.

I'm using Gmaps4Rails to convert latitude- and longitude-values to markers:

# /app/controllers/buildings_controller.rb
def index
  @buildings = Building.all.order(:id)
  @hash = Gmaps4rails.build_markers @buildings do |building, marker|
    marker.json ({id:, name:}) # we'll need the building's ID later! building.latitude
    marker.lng building.longitude
    marker.infowindow render_to_string(partial: "/buildings/infowindow", locals: {object: building})

I first used this example to build the list of links and it was working fine. But then I wanted to style the list a bit and make it responsive. I decided to use Grid-A-Licious - a grid prettifier written in jQuery. I used <div class="grid-item"> to wrap my building-names inside, but this totally broke the link between a building-name and its marker on the map, because Gmaps4Rails' function bindLiToMarker() binds the marker to an object, but this object vanishes when the user resizes the browser window. And the object vanishes because Grid-A-Licious rebuilds each grid-item according to the new window's size. Bummer!

Here is my solution

First, generate the map and the grid with building-names separately:

<!-- /app/views/buildings/index.html.erb -->

<div class="container">
<div id="buildings-map" style='width: 100%; height: 450px;'></div>

<div class="container">
<div id="buildings-grid"></div>

<script type="text/javascript">
   buildingsMap( <%= raw @hash.to_json %> );
   buildBuildingsList( <%= raw @hash.to_json %> );
</script><br />
/* /app/assets/javascripts/application.js.erb */ = {};

function buildingsMap(json_array){ ='Google');{ provider:{}, internal: {id: 'buildings-map'}}, function(){ =;

    _.each(json_array, function(json, index){
      marker =[index];
      marker.serviceObject.set('id',; // those are the buildings' IDs
      json.marker = marker;

function buildGridItem(json){
 div = document.createElement('div');
 div.setAttribute('class', 'grid-item');
 div.setAttribute('id',; // we give each div the ID of the building, it represents
 div.innerHTML = ("<a href='#map-anchor'><span class='glyphicon glyphicon-map-marker'> " + + "</a>");

 return div;

function buildBuildingsList(json_array){
  _.each(json_array, function(json){
    $address = $( buildGridItem(json) );
  // grid-a-licious funtion settings
    gutter: 20,
    width: 250,
    selector: '.grid-item'

Second, when the document has loaded or whenever the browser window has resized, link the grid-item again to the marker:

<!-- /app/views/buildings/index.html.erb -->

<script type="text/javascript">


_.debounce() is a method from underscore.js and it waits until all resize calls on the browser window has been executed.

/* /app/assets/javascript/application.js.erb */

function findMarker(i){
  for (var l=0; l<; ++l){
    marker =[l];
    if (marker.getServiceObject().id == i ) {
      return marker;

function bindGridItemsToMarker(){
  $('.grid-item').on('click', function(){
    i = $(this).attr('id');

    marker = findMarker(i);;
    marker.setMap(; //because clusterer removes map property from marker
    google.maps.event.trigger(marker.getServiceObject(), 'click');;

What we basically do is, whenever the user clicks on the link (in my case the building's name), we extract the grid-item's id-attribute, find the marker on the map with the same id and click on it. And that's it!

Here is the result and here is the code to our Coding Da Vinci project on GitHub.