Skip to content

Instantly share code, notes, and snippets.

@jerefrer
Created December 27, 2014 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerefrer/b9d3acb553ead86b3c1d to your computer and use it in GitHub Desktop.
Save jerefrer/b9d3acb553ead86b3c1d to your computer and use it in GitHub Desktop.
Gmaps4Rails - Client side rendering of infowindows
// app/views/users/index.html.haml
= javascript_include_tag '//maps.google.com/maps/api/js?v=3.13&sensor=false&libraries=geometry'
= javascript_include_tag '//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js'
#map{style: "height: 400px;"}
:javascript
window.load_google_map('map', #{@map_markers})
# app/assets/javascripts/load_google_map.js.coffee
class UserInfowindow extends Gmaps.Google.Builders.Marker
user_infowindow_html: ->
"<div>
<div class='imgRounded'>
<a href='#{@args.user_path}'><img alt='#{@args.username}' src='#{@args.user_avatar_path}'></a>
</div>
<h2>
<a href='#{@args.user_path}'>#{@args.username}</a>
</h2>
<div>
<a href='#{@args.user_path}'>See his profile
<i class='glyphicon glyphicon-chevron-right'></i>
</a>
</div>
</div>"
create_infowindow: ->
new(@primitives().infowindow)({content: @user_infowindow_html() })
window.load_google_map = (id, map_markers) ->
handler = Gmaps.build 'Google', { builders: { Marker: UserInfowindow} }
handler.buildMap { provider: {}, internal: {id: id} }, ->
markers = handler.addMarkers(map_markers)
handler.bounds.extendWith(markers)
handler.fitMapToBounds()
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
@map_markers = build_markers(User.geocoded)
end
private
def build_markers(users)
Gmaps4rails.build_markers(users) do |user, marker|
marker.lat user.latitude
marker.lng user.longitude
marker.json({
user_path: user_path(user),
user_avatar_path: user.avatar.big.url,
username: user.username
})
end.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment