Search code examples
ruby-on-railsruby-on-rails-3mapsgmaps4rails

Google Maps For Rails replaceMarkers not working


Am I missing something obvious? Map keeps loading @oldjson which I set in the controller and won't be replaced when I set it in the view.

View code:

<% @json = Map.find_by_id('39').to_gmaps4rails %>
<%= gmaps("markers" => {"data" => @oldjson, "options" => { "draggable" => true } } ) %>
<script>    
  Gmaps.map.replaceMarkers(<%= @json %>);
</script>

Thanks.


Solution

  • I guess you"re facing a js error with this current code.

    The reason is the following:

    • the js used and created by gmaps4rails is put within the yield :scripts

    • so your additionnal js here is called before the map is created

    Solution:

    <% @json = Map.find_by_id('39').to_gmaps4rails %>
    <%= gmaps("markers" => {"data" => @oldjson, "options" => { "draggable" => true } } ) %>
    <% content_for :scripts do %>
     <script>    
      Gmaps.map.replaceMarkers(<%= @json %>);
      </script>
    <% end %>