我在将地图加载到heroku时遇到问题,该地图在本地正常运行,但是当我在heroku上对其进行测试时,Web控制台显示错误为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=®ion=" 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>
所以我只把
yield: javascript
放在javascript_include_tag
之后,所以我不确定问题是什么 最佳答案
在文档完成加载和所有Javascript文件加载之前,可能已调用buildMap(<%=raw @hash.to_json %>);
。尝试包装在document.ready函数中:
$(document).ready(function() {
buildMap(<%=raw @hash.to_json %>);
});
否则,定义
RichMarkerBuilder
的coffeescript文件将无法正确编译到Heroku上的资产中。您可以从控制台运行buildMap({})
吗?如果不是,则需要确保已使用资产管道正确编译了JS文件。