首页 文章

Ruby on Rails:使用gmaps4rails进行迭代

提问于
浏览
0

我按照gmaps4rails中的说明创建了一个简单的应用程序,您可以使用 MANY 标记加载 ONE Map . 我有兴趣用 one 标记加载 many Map . 当我尝试将代码放在迭代中时,div只保持空...在视图中:

<p id="notice"><%= notice %></p>

<h1>Listing Places</h1>

<table>
  <thead>
    <tr>
      <th>Latitude</th>
      <th>Longitude</th>
      <th>Address</th>
      <th>Description</th>
      <th>Title</th>
      <th>Map</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @places.each do |place| %>
      <tr>
        <td><%= place.latitude %></td>
        <td><%= place.longitude %></td>
        <td><%= place.address %></td>
        <td><%= place.description %></td>
        <td><%= place.title %></td>
        <td><div style='width: 80px;'>
            <div id="minimap" style='width: 80px; height: 40px;'></div>
          </div>

          </td>
        <td><%= link_to 'Show', place %></td>
        <td><%= link_to 'Edit', edit_place_path(place) %></td>
        <td><%= link_to 'Destroy', place, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Place', new_place_path %>

<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>

<script type="text/javascript">
  handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers(<%=raw @hash.to_json %>);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
});
</script>

在控制器中我有这个代码:

def index
    @places = Place.all
    @hash = Gmaps4rails.build_markers(@places) do |place, marker|
      marker.lat place.latitude
      marker.lng place.longitude
end

我已经尝试将脚本放在循环中并将标记更改为

markers = handler.addMarkers("lag":<%= place.latitude %>,"lng":<%= place.longitude %>);

但它不起作用 .

有人能帮助我吗?这是我的存储库:https://github.com/francisconlm/mapps告诉我你是否需要更多细节谢谢

ps:如果有人认为使用gmaps api比使用gmaps4rails有更好的方法,建议总是欢迎:)

2 回答

  • 1

    我找到了答案 . 我还需要循环变量 . 这对我有用,但也许有人可以告诉我如何改进它:)

    <% @places.each do |place| %>
            <tr>
              <td><%= place.latitude %></td>
              <td><%= place.longitude %></td>
              <td><%= place.address %></td>
              <td><%= place.description %></td>
              <td><%= place.title %></td>
              <td>
    
      <div style='width: 400px;'>
                  <div id="map<%= place.id %>" style='width: 400px; height: 200px;'></div>
                </div>
    
      <script type="text/javascript">
        handler<%= place.id %> = Gmaps.build('Google');
        handler<%= place.id %>.buildMap({ provider: {}, internal: {id: 'map<%= place.id %>'}}, function(){
        markers<%= place.id %> = handler<%= place.id %>.addMarkers([{ lat: <%= place.latitude %>, lng: <%= place.longitude %>}]);
        handler<%= place.id %>.bounds.extendWith(markers<%= place.id %>);
        handler<%= place.id %>.fitMapToBounds();
        handler<%= place.id %>.getMap().setZoom(10);
        });
      </script>
              </td>
              <td><%= link_to 'Show', place %></td>
              <td><%= link_to 'Edit', edit_place_path(place) %></td>
              <td><%= link_to 'Destroy', place, method: :delete, data: { confirm: 'Are you sure?' } %></td>
            </tr>
          <% end %>
    
  • 0

    你必须使用它

    markers = handler.addMarkers(<%=raw @hash.to_json %>);
    

    你不能在你的div里面循环 . 请查看firebug,即js上的任何错误 .

相关问题