Skip to content
bodaonline edited this page Nov 28, 2015 · 16 revisions

Since 1.2.0, you can pass your maps params almost entirely from controller.

That's really useful when it comes to use partials as infowindows and I extended this to the other features.

From 1.4.0, no need to escape characters anymore as Rails to_json will do it for you behind the scene.

Previously, you could just add extra content to the json created:

@json = User.all.to_gmaps4rails do |user, marker|
  "\"id\": #{user.id}"
end

Now, do:

@json = User.all.to_gmaps4rails do |user, marker|
  marker.json "\"id\": #{user.id}, \"foo\": #{user.bar}"
end

Or even better (and this is how you'll have to do it in v2.0):

@json = User.all.to_gmaps4rails do |user, marker|
  marker.json({ :id => user.id, :foo => user.bar })
end

Here is an example which speaks for itself:

@json = User.all.to_gmaps4rails do |user, marker|
  marker.infowindow render_to_string(:partial => "/users/my_template", :locals => { :object => user})
  marker.picture({
                  :url => "http://www.blankdots.com/img/github-32x32.png",
                  :width   => 32,
                  :height  => 32
                 })
  marker.title   "i'm the title"
  marker.sidebar "i'm the sidebar"
  marker.json({ :id => user.id, :foo => "bar" })
end

In the view, one would then access the user object passed in by locals. "TODO: Include example for using the user object in the partial template."

Example to use with user object (no partial template) just this in the controller:

def home 
....
@hash = Gmaps4rails.build_markers(@allrenters) do |user, marker|
user_path = view_context.link_to user.username, user_path(user)
marker.lat user.latitude
marker.lng user.longitude
marker.infowindow "<b>#{user_path}</b>"
....
end

Note - Make sure you are serving up the width and height parameters as type int rather than string when using custom markers. Otherwise you run into issues with tiling issues in newer versions of the gmaps api (see: https://github.com/apneadiving/Google-Maps-for-Rails/issues/183).