【问题标题】:how to add multiple maps into different pages of a rails application using gmaps4rails?如何使用 gmaps4rails 将多个地图添加到 rails 应用程序的不同页面?
【发布时间】:2014-02-09 06:18:55
【问题描述】:

我尝试了多种方法来尝试让地图显示在我家的 index.html.erb 和位置的 index.html.erb 上,但注意到似乎工作正常。我的总体目标是能够在我的位置页面中编辑内容,但只显示内容而不在主页上编辑任何内容。任何帮助表示赞赏。谢谢!

我现在的家index.html.erb(注意:我只显示javascript):

<script type="text/javascript">
  $(function(handler) {
    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>

我的位置index.html.erb

<script type="text/javascript">
  $(function(handler) {
    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>

对于这两个控制器,我的 index 方法中也有这个:

  def index
    @locations = Location.all
    @hash = Gmaps4rails.build_markers(@locations) do |location, marker|
        marker.lat location.latitude
        marker.lng location.longitude
        marker.infowindow location.summary
       marker.json({title: location.title })
       end
  end

顺便说一句,我有一个模型location,它提供了纬度、经度、摘要、标题。

【问题讨论】:

  • 您有什么问题?有什么错误吗?代码工作见:apneadiving.github.io 所以你必须在这里提供更多信息。我怀疑你的坐标为零
  • 当我只有位置控制器时,地图工作得很好,但后来我添加了一个家庭控制器并尝试在那里制作第二张地图,但首先它不再存储位置,它只存储空白空间(在列出新添加的位置时,它只列出了空格)并且地图也不会显示在主页中。
  • 任何js错误?不可能猜到这里出了什么问题
  • 没有js错误。它可能是您之前提到的 nil 坐标,因为当我添加新位置时它会将空格存储到数据库中。我在我的家庭和位置控制器中使用相同的变量。我尝试将 internal: { id: 'map' } 更改为在两个类中都不同,但它仍然没有在主页中显示地图,也无法添加位置。
  • 如果你输入 map 它应该匹配你的 dom 中的一个 id。否则你有一个 CSS 问题

标签: javascript ruby-on-rails ruby ruby-on-rails-4 gmaps4rails


【解决方案1】:

gmaps4rails 不适用于某些 gem,我必须删除一些才能停止存储空字符串。之后,我还将主视图中的处理程序更改为 handler2 以使其工作。谢谢你

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 2013-01-29
    相关资源
    最近更新 更多