Search code examples
ruby-on-railsruby-on-rails-3google-mapsgoogle-maps-api-3gmaps4rails

Refresh div with gmaps4rails (AJAX)


I'm using Rails 3.0.9 version, and jquery. I've been using this gem without a database. It is used only for map display, and display KML file on it. For this I used:

<div id='ajax_map'>
    <% @kmlurl="http://mysite/file1.kml" %>
    <%= gmaps( :kml => { :data => "[{ url: #{@kmlurl.inspect}}]" } ) %>
</div>

All great shows.

I want to do that after you change the links (@ kmlurl), and click on the button, the map updated with this new KML file. I use a separate action js.erb with the following code:

$('#ajax_map').html('<%= @kmlurl="http://mysite/file2.kml" %>'+'<br />'+'<%= gmaps( :kml => { :data => "[{ url: #{@kmlurl.inspect}}]" } ) %>');

But he does not update the DIV. "js.erb" rendered normally, without using the method of gmaps () it normally returns @ kmlurl. I tested this same code in the ". Html.erb" in the tags , it loads a new file, but, of course, just when the page loads. How can I solve this problem?


Solution

  • Solved the problem as follows (in js.erb):

    $('#ajax_map').html('<%= escape_javascript( gmaps({:last_map => false}) ) %>');
    
    Gmaps.map = new Gmaps4RailsGoogle();
    Gmaps.load_map = function() {
        Gmaps.map.map_options.maxZoom = 15;
        Gmaps.map.initialize();
    Gmaps.map.kml = [{ url: '<%= "#{@kmlurl}" %>'}];
    Gmaps.map.create_kml();
        Gmaps.map.adjustMapToBounds();
        Gmaps.map.callback();
    };
    Gmaps.loadMaps();