Skip to content

Instantly share code, notes, and snippets.

@apneadiving
Created January 20, 2012 00:25
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save apneadiving/1643990 to your computer and use it in GitHub Desktop.
Save apneadiving/1643990 to your computer and use it in GitHub Desktop.
gmaps4rails: Don't show map by default and load markers with ajax
class UsersController < ApplicationController
respond_to :html, :json
def index
@json = User.all.to_gmaps4rails
respond_with @json
end
end
<!-- 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>
@steventen
Copy link

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

@steventen
Copy link

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

@pedroassumpcao
Copy link

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

@alexwiththescar
Copy link

What changes would i make to this for v2? Cant seem to get it working. Its complaining about Gmaps.loadMaps(); as undefined Function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment