Search code examples
gmaps4rails

gmaps4rails v2 - customize sidebar


I use gmaps4rails to display markers on a map and i have a sidebar witch allow me to select a marker and open it on my map.

the sidebar wirks well but i would like to customize it.

Right know i do:

<script type="text/javascript">

  $(document).ready(function(){
    var raw_markers = <%=raw @hash.to_json %>;

    function createSidebarLi(json){
      return ("<li>" + json.titre + ' ' + json.address + "</li>");
    };

    function bindLiToMarker($li, marker){
      $li.on('click', function(){
        handler.getMap().setZoom(14);
        marker.setMap(handler.getMap()); //because clusterer removes map property from marker
        marker.panTo();
        google.maps.event.trigger(marker.getServiceObject(), 'click');
      });
    };

    function createSidebar(json_array){
      _.each(json_array, function(json){
        var $li = $( createSidebarLi(json) );
        $li.appendTo('#markers_list');
        bindLiToMarker($li, json.marker);
      });
    };

    handler = Gmaps.build('Google', { builders: { Marker: InfoBoxBuilder} });
    handler.buildMap({ internal: {id: 'map'}}, function(){

      var markers = handler.addMarkers(raw_markers);

      _.each(raw_markers, function(json, index){
        var marker = markers[index];
        json.marker = marker;
        google.maps.event.addListener(handler.getMap(), "click", function(){
          infoWindow.close();
        });
        google.maps.event.addListener(marker.getServiceObject(), 'mouseover', function(){
          google.maps.event.trigger(marker.getServiceObject(), 'click');
        });
      });


      createSidebar(raw_markers);
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();
    });

  });

</script>

I'd like to create a partial and display it instead of just:

return ("<li>" + json.titre + ' ' + json.address + "</li>");

In my partial, i'd like to do the same as my custom infowindow.

IN my controller, i use:

marker.infowindow render_to_string(:partial => "/properties/infowindow", :locals => { :property => property})

Is there a way to do that with the sidebar ? I used to do "marker.sidebar render_to_string" it with gmaps4rails v1 but it doesn't work anymore.


Solution

  • Its just javascript.

    First, provide the data in your controller, where you generate json (I assume you use the builtin json generator):

    marker.json({
      sidebar: render_to_string(:partial => "/properties/infowindow", :locals => { :property => property})
    })
    

    Then display it:

    function createSidebarLi(json){
      return json.sidebar;
    };