public
Last active

gmaps4rails: Don't show map by default and load markers with ajax

  • Download Gist
users_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10
class UsersController < ApplicationController
 
respond_to :html, :json
 
def index
@json = User.all.to_gmaps4rails
respond_with @json
end
 
end
view.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<!-- create html + load js files but don't create map itself: will be done after ajax call -->
<%= gmaps({:last_map => false}) %>
 
<!-- button to trigger ajax call -->
<button type="button" id="ajax">Load Map</button>
 
<script type="text/javascript" charset="utf-8">
$(function() {
 
//hide the empty container
$(".map_container").hide();
 
$("#ajax").click(function(){
 
$.getJSON('/users', function(json){
$(".map_container").show();
Gmaps.loadMaps();
Gmaps.map.addMarkers(json);
})
})
 
});
</script>

Hi, how can I pass options for markers into addMarkers method?

OK, I figured out. No worries, @apneadiving

Instead of

<%= gmaps({:last_map => false}) %>

I can add options this way

<%= gmaps("markers" => {"data" =>"[]", "options" => {"randomize" => true, "max_random_distance" => 2, "do_clustering" => true}}, "map_options" => { "provider" => "google", "provider_key" => $GOOGLE_API_KEY, "auto_adjust" => true }, :last_map => false) %>

In which, I used "[]" to fill the data section

Is there a way to create the sidebar marker list with plain html?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.