Search code examples
ruby-on-railsajaxgoogle-mapsruby-on-rails-4gmaps4rails

Ajax reloading in Gmaps4rails


I'm using Gmaps4rails gem in my Rails 4 application. I have a geo data being loaded into the map, filtered by Ransack (search filter gem) and Kaminary (pagination filter gem). Both are working very well with AJAX loading.

However, I have no idea how to update the map when I change the information on the filters.

This is index.js.erb:

$('#sidebar').html('<%= escape_javascript render(@tweets) %>');
$('#paginator').html('<%= escape_javascript(paginate(@tweets, :remote => true).to_s) %>');

This is the first time that I am using ajax in a Rails application.


Solution

  • As @apneadiving suggested, the use of JS methods for Gmaps4rails will work, like this:

    handler.buildMap( {provider: mapOptions, internal: {id: 'map'}}, function(){
        markers = handler.removeMarkers(<%=raw @hash.to_json %>);       
    });
        handler.buildMap( {provider: mapOptions, internal: {id: 'map'}}, function(){
        markers = handler.addMarkers(<%=raw @hash.to_json %>);      
    });
    

    However, I found a more elegant solution, quite simple as well, using jQuery and a partial _map.erb.js containing the handler events. In the index.js.erb:

    $('#map').html('<%= escape_javascript render("map") %>');