Nick
Nick

Reputation: 8493

Trouble getting Gmaps4rails callbacks working

I am unable to get a Javascript callback to work with gmaps4_rails when the map has loaded or is in the 'idle' state.

Based on the wiki and this other SO question Dynamically load Google Maps Markers with gmaps4rails I thought this would fire all of the alerts in this view:

<%= gmaps(:map_options => {:detect_location => true,
                           :center_on_user => true,
                           :auto_zoom => true,
                           :zoom => 12,
                           :auto_adjust => false},
           :markers => {:data => @json,   
                        :options => {:processsing => "@json",
                                     :list_container => "markers_list",
                                     :do_clustering => true
                                                       } }) %>
<ul id="markers_list"> </ul>

<script type="text/javascript" charset="utf-8">
  alert("About to register callback");
  Gmaps.map.callback = function() {
    alert("Map Loaded Callback Reached");
  }

  function gmaps4rails_callback() {
     alert("gmaps4_rails callback");
  }
</script>

<%= yield :scripts %>

Instead I only get the "about to register callback" alert. The other functions never seem to be called. I have a feeling I'm misunderstanding something about the namespace but any insight would be appreciated- I'm trying to get things setup to refresh from a JSON endpoint as the map moved but I'm not having a lot of luck getting past this first step of making sure I can trigger a callback.

I should note the map appears normally and my initial data is all there.

Upvotes: 1

Views: 911

Answers (1)

apneadiving
apneadiving

Reputation: 115531

As per documentation here, you must wrap the js code in a content_for :scripts

Why?

because gmaps4rails js code should be loaded first.

Upvotes: 1

Related Questions