1

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&amp;sensor=false&amp;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.

1 Answers1

0

Try to run the script on dom ready, using jquery it's:

$(function() {
  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();
  });
})
apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • I have the same problem and your answer doesn't solve the problem, unfortunately. :/ I still receive this error: `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. js?v=3.13&sensor=false&libraries=geometry:8` – Fei Jun 22 '15 at 21:43
  • I'm pretty sure it has nothing to do with the gem, comment it to check – apneadiving Jun 22 '15 at 22:03
  • The answer [here](http://stackoverflow.com/questions/19905972/gmaps-is-not-defined-in-rails-4-gmaps4rails-2-0-3) by [deakolt](http://stackoverflow.com/users/888809/deakolt) solved my problem! – Fei Jun 22 '15 at 22:30