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

addMarkers(@marker) not working for dynamic content


I'm a relative noob, but have been having fun integrating gmaps4rails in my project. I have come across an issue that might be a bug in the gem, or it might simply be my ignorance, but I'm hoping someone can point me in the right direction.

I have a home page that includes a jquery tab ui element. One tab contains a list view of stories and the second tab contains a map view of those stories. I am using gmaps4rails in the map view.

On page reload all of the stories that meet my search criteria appear in my list view and map view as expected. My problems come when I try to use Gmaps.map.addMarkers().

Let's say I add a new story that meets my search params. My stories#create action sets the @story variable as expected, and then I set the @marker variable as follows:

@markers = @story.to_gmaps4rails

Then my create.js.erb view inserts the @story appropriately into my list tab view. Unfortunately, Gmaps.map.addMarkers(@marker) does not insert the marker into the map tab view.

Here is some more relevant code:

story.rb:

class Story < ActiveRecord::Base

  acts_as_api
  acts_as_gmappable :process_geocoding => false

  def gmaps4rails_marker_picture
    {
      "rich_marker" => "<div class='story_marker' id=marker_for_story_#{self.id }  ><img height='30' width='30' src='http://farm4.static.flickr.com/3212/3012579547_097e27ced9_m.jpg'/></div>"
    }
  end

create.js.erb:

$("#stories_list").prepend("<%=raw escape_javascript(render(@story)) %>");  //put new story in list view
<%= logger.debug("!!!!!!!!!! gmaps json array: #{@marker}")%> 
Gmaps.map.addMarkers(@marker); 

home.html.erb:

<div id="map-tab">
    <%= yield :head %>  <!-- for gmaps4rails -->
    <%= gmaps("markers" => { "data" => @json, "options" => { "clusterer_gridSize" => 5, "clusterer_maxZoom" => 13 } }) %>
</div>

When i check the console after calling create.js.erb I see that @marker does contain the proper json array info. I can copy that info straight from the console and then use the firebug console to run addMarkers(copied json array info), and the marker shows up as expected. Also, I can modify the addMarkers command in my create.js.erb file to look like the following, which also works: Gmaps.map.addMarkers([{"lng": "<%[email protected]%>", "lat": "<%[email protected] %>"}]); But, I'd rather use the to_gmaps4rails helper.

Thanks for any help you can give me to solve this mystery, and happy holidays!


Solution

  • I guess you should simply replace:

    Gmaps.map.addMarkers(@marker); 
    

    With:

    Gmaps.map.addMarkers(<%= @marker %>);