gmap4rails 在本地工作但不显示在 heroku 上

gmap4rails worked locally but doesn't show on heroku

我在 heroku 上加载地图时遇到问题,地图在本地工作正常,但是当我在 heroku 上测试它时,网络控制台显示错误是 buildMap is not define

错误与此行有关buildMap(<%=raw @hash.to_json %>);

这是我的观点:

<script src="//maps.google.com/maps/api/js?v=3.23&sensor=false&client=&key=&libraries=geometry&language=&hl=&region=" type="text/javascript"></script> 
<script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js" type="text/javascript"></script>
<script src='//cdn.rawgit.com/printercu/google-maps-utility-library-v3-read-only/master/infobox/src/infobox_packed.js' type='text/javascript'></script> <!-- only if you need custom infoboxes -->
<script src='//cdn.rawgit.com/googlemaps/js-rich-marker/gh-pages/src/richmarker-compiled.js' type='text/javascript'></script>
<script src='//cdn.rawgit.com/printercu/google-maps-utility-library-v3-read-only/master/infobox/src/infobox_packed.js' type='text/javascript'></script>

<% if customer_signed_in? %>
   <nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <%= link_to "Discount Now", dashboard_path, class: "navbar-brand", id: "logo" %>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li> <%= link_to "Home", dashboard_path %> </li>
        <li> <%= link_to "edit profile", edit_customer_registration_path %></li>
      </ul>

      <ul class="nav navbar-nav navbar-right">
      <li>Signed in as <%= current_customer.email %></li>
        <li><%= link_to "log out", destroy_customer_session_path,:method => :delete, id: "nav-login-button", type: "button", class: "btn btn-primary navbar-btn"%></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>


<%= form_tag dashboard_path, :method => :get do %>
<div class= "row">
  <p>
    <%= text_field_tag :search, params[:search], class: "col-md-4"%>
    <%= submit_tag "Search Near", class: "btn btn-info", :name => nil  %>
  </p>
  </div>
<% end %>

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

<script type="text/javascript">
  buildMap(<%=raw @hash.to_json %>);

</script>
<%else %>
 <% link_to "sign up", new_customer_session_path%>
<% end %>

以及我构建地图的咖啡脚本:

class RichMarkerBuilder extends Gmaps.Google.Builders.Marker #inherit from builtin builder

  # override method
  create_infowindow: ->
    return null unless _.isString @args.infowindow

    boxText = document.createElement("div")
    boxText.setAttribute("class", 'white') #to customize
    boxText.innerHTML = @args.infowindow
    #boxText.outerHTML = @args.title
    @infowindow = new InfoBox(@infobox(boxText))

    @bind_infowindow() 

  infobox: (boxText)->
    content: boxText
    pixelOffset: new google.maps.Size(-140, 0)
    boxStyle:
      width: "280px"

@buildMap = (markers)-> 
  handler = Gmaps.build 'Google', { builders: { Marker: RichMarkerBuilder} } #dependency injection

  #then standard use
  handler.buildMap { provider: {}, internal: {id: 'map'} }, ->
    markers = handler.addMarkers(markers)
    handler.bounds.extendWith(markers)
    handler.fitMapToBounds()
    handler.bounds.extendWith markers

这是我的 application.html.erb 文件

<!DOCTYPE html>
<html>
<head>
  <title>CS160</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
<%= yield :javascript %>
</body>
</html>

所以我只在javascript_include_tag之后放了yield: javascript,所以我不确定是什么问题

可能 buildMap(<%=raw @hash.to_json %>); 在文档加载完成之前被调用,并且您的所有 Javascript 文件都已加载。尝试包装在 document.ready 函数中:

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

否则,定义 RichMarkerBuilder 的 coffeescript 文件未正确编译到您在 Heroku 上的资产中。您可以从控制台 运行 buildMap({}) 吗?如果没有,您需要确保您的 JS 文件正在使用资产管道正确编译。