简体   繁体   English

gmap4rails在本地工作,但在heroku上没有显示

[英]gmap4rails worked locally but doesn't show on heroku

I'm having problem getting the map to load on heroku, the map is working fine locally, but when I test it on heroku, the web console said that the error is buildMap is not define 我在将地图加载到heroku时遇到问题,地图在本地工作正常,但是当我在heroku上测试时,Web控制台说错误是buildMap is not define

the error is relate to this line buildMap(<%=raw @hash.to_json %>); 该错误与此行buildMap(<%=raw @hash.to_json %>);

this is my view: 这是我的看法:

<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 %>

and the coffee script where i build the map: 和构建地图的咖啡脚本:

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

this is my application.html.erb file 这是我的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>

so I did put yield: javascript only after javascript_include_tag , so I'm not sure what the issue is 所以我确实在javascript_include_tag之后放了yield: javascript ,所以我不确定是什么问题

It's possible that buildMap(<%=raw @hash.to_json %>); buildMap(<%=raw @hash.to_json %>); is being called before the document finishes loading and all your Javascript files are loaded. 在文档完成加载之前调用,并加载所有Javascript文件。 Try wrapping in a document.ready function: 尝试包装document.ready函数:

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

Otherwise, the coffeescript file defining RichMarkerBuilder is not being properly compiled into your assets on Heroku. 否则,定义RichMarkerBuilder的coffeescript文件无法正确编译到Heroku上的资源中。 Can you run buildMap({}) from the console? 你可以从控制台运行buildMap({})吗? If not, you'll need to make sure your JS files are properly being compiled with the asset pipeline. 如果没有,您需要确保使用资产管道正确编译JS文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM