So, I must admit I'm a newbie to rails, so I fear I might be missing something extremely obvious, but I keep getting error messages with the Gmaps4Rails gem. Looking around, I can't find this problem anywhere so I think I've forgotten something so fundamentally important that no-one else seems to have this issue.
I know I'm close because whenever I refresh the page the whole thing magically works, so, I know its an issue of timing. I'm also using Bootstrap, if that helps.
The error message I keep getting is:
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
View Code:
<strong>Map:</strong>
<div id="map-canvas"/></div>
<% content_for :scripts do %>
<script src="//maps.google.com/maps/api/js?v=3.13&sensor=false&libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map-canvas'}}, function(){
markers = handler.addMarkers(<%= raw @hash.to_json %>)
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
<% end %>
Application.html.erb code:
<body>
<ul class="nav nav-tabs">
<li><%= link_to 'Services', services_path %></li>
<li><%= link_to 'Users', users_path %></li>
<li><%= link_to 'Sign In', user_sessions_new_path %></li>
<li><%= link_to "Log Out", logout_path, method: :delete %></li>
<li>You are signed in as : <%= session[:username] %></li>
</ul>
<%= yield %>
<%= yield :scripts %>
</body>
Controller code:
def index
@services = Service.all
@hash = Gmaps4rails.build_markers(@services) do |service, marker|
marker.lat service.lat
marker.lng service.long
end
end
So, some of the things I have tried, have included:
- Including 'callback=initialize' into the google maps api script, and subsequently calling onload after the page has loaded.
- Putting the scripts in just about every place you could imagine
And a fair few others from around the internet, but none of them work, sigh. Please advise.