apneadiving / Google-Maps-for-Rails

Enables easy Google map + overlays creation in Ruby apps
https://apneadiving.github.io/
MIT License
2.26k stars 382 forks source link

Cannot Update Markers #118

Closed RozKen closed 12 years ago

RozKen commented 12 years ago

Hello I'm Ken. And I'm new to RubyOnRails. I apologize if this is an awkward question in here.

I'd like to find display markers which are the nearest to the address input on form. But the markers cannot update.

I suspect 'Gmaps.loadMaps();'. I read source code of index.html in my browser, there are javascripts which are including data acquired from SQL. I suppose the data should not be static.

How can I update markers on Google Maps? Thanks.

Here are my code structure

===index.html.erb=== <%= form_tag '/nearest/change', :remote => true, :method => 'GET' do %> <%= text_field "search", "address", "size" => 20 %> <%= submit_tag 'submit' %> <% end %>

<%= render 'list' %> <%= render 'map' %>

=== _list.html.erb ===

...
address

=== _map.html.erb ===

<%= gmaps4rails(@map_data) %>

=== change.js.erb === $("#list").html("<%= escape_javascript(render :partial => 'list', :object => @data)%>"); $("#map_location").html("<%= escape_javascript(render 'map') %>"); Gmaps.loadMaps();

=== nearest_controller.rb === def change @data = Nearest.where(:id => 1) #easiest filtering @map_data = @data.to_gmaps4rails end

apneadiving commented 12 years ago

You could have read the doc:https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Javascript-goodies

Markers

If you want to replace the current markers with your new ones:

Gmaps.map.replaceMarkers(your_markers_json_array); //this is a JS function

If you want to add markers to those already displayed (it doesn't check duplicates):

Gmaps.map.addMarkers(your_markers_json_array);     //this is a JS function
RozKen commented 12 years ago

Thanks a lot! I'll try it on!

I've already read that topic, but I believed that there is a ruby written wrapper which provide replacing markers. Thanks.

RozKen commented 12 years ago

I can get replacing the markers. I learned the way to send json array.

On Controller

@data = YourDatabase.All
@map_data = @data.to_gmaps4rails

On View

Gmaps.map.replacemarkers(<%=raw @map_data %>);

I took time for finding 'raw' command.

Thank you.

khanhnguyen commented 12 years ago

Thanks. It's worked for me