Gmaps4rails как создать список маркеров

Мой код:
Местоположение:

class Location < ActiveRecord::Base
  attr_accessible :latitude, :longitude, :timestamp, :user_id, :user
  validates :latitude, :longitude, :timestamp, :user, :presence => true
  belongs_to :user

  def gmaps4rails_sidebar
    "<span>#{user}</span>"
  end
end

Контроллер:

@locations = []
User.all.each do |user|
    if user.role?('manager')
        last_location = user.locations.last
      if last_location        
        location = {}
        location[:lat] = last_location.latitude
        location[:lng] = last_location.longitude
        location[:title] = last_location.timestamp
        location[:infowindow] = "<b>#{t(:datetime)}:</b> #{last_location.created_at} <br /><b>#{t(:latitude)}:</b> #{last_location.latitude}<br /><b>#{t(:longitude)}:</b> #{last_location.longitude}<br /><b>#{t(:user)}:</b> #{user.username}"
        @locations << location
      end
    end
end

Просмотр:

<%= javascript_include_tag 'locations' %>
<div class="map_container" style='width: 980px; height: 458px;'>
  <div id="map" style='width: 980px; height: 458px;'></div>
</div>
<ul id="markers_list">  </ul>
<script type="text/javascript">
    jQuery(document).ready(function() {
      handler = Gmaps.build('Google', { markers: { maxRandomDistance: null } });
      handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
        markers = handler.addMarkers(<%=raw @locations.to_json %>);
        handler.bounds.extendWith(markers);
        handler.fitMapToBounds();
        handler.getMap().setZoom(12);
      });
    });
</script>

Я хочу отобразить список маркеров. Я читал, как это было сделано в старой версии gem. Но не знаю, как это сделать в новой версии. Подскажите, как гем в новой версии можно выносить маркеры в отдельный список?


person galievruslan    schedule 14.02.2014    source источник
comment
apneadiving.github.io   -  person apneadiving    schedule 14.02.2014
comment
Да, я уже нашел решение. Но не могу опубликовать :(   -  person galievruslan    schedule 14.02.2014
comment
Тогда закрой свой вопрос :)   -  person apneadiving    schedule 16.02.2014


Ответы (1)


Я нахожу пример http://apneadiving.github.io/

<script type="text/javascript">
    jQuery(document).ready(function() {
        var locations_json = <%=raw @locations.to_json %>
      handler = Gmaps.build('Google', { markers: { maxRandomDistance: null } });
      handler.buildMap({ provider: {}, internal: {id: 'map'}, list_container: "markers_list"}, function(){
        markers = handler.addMarkers(locations_json);
        handler.bounds.extendWith(markers);
        handler.fitMapToBounds();
        handler.getMap().setZoom(12);

        _.each(locations_json, function(json, index){
            json.marker = markers[index];
          });

          createSidebar(locations_json);
      });


      function createSidebarLi(json){
          return ("<li><a>" + json.name + "</a></li>");
        };

        function bindLiToMarker($li, marker){
          $li.on('click', function(){
            handler.getMap().setZoom(14);
            marker.setMap(handler.getMap());
            marker.panTo();
            google.maps.event.trigger(marker.getServiceObject(), 'click');
          })
        };

        function createSidebar(json_array){
          _.each(locations_json, function(json){
            var $li = $( createSidebarLi(json) );
            $li.appendTo('#markers_list');
            bindLiToMarker($li, json.marker);
          });
        };
    });
</script>
person galievruslan    schedule 17.02.2014