使用原型动态加载时,gmaps4rails地图未显示

我有一个视图有3个选项卡,每个选项卡代表数据视图(表,地图,图表)。 当我单击选项卡时,如果运行switch_view.js.rjs,它将使视图失明

{:controller => :biogas_calculator, :action => :switch_view, :view => "table"}, :method => :get, :loading => visual_effect(:blind_up, "biomass_configuration", :duration => 0.5, :queue => 'front'), :html => {:id => "table_view", :class => "toggle_map_button"} %> {:controller => :biogas_calculator, :action => :switch_view, :view => "map"}, :method => :get, :loading => visual_effect(:blind_up, "biomass_configuration", :duration => 0.5, :queue => 'front'), :html => {:id => "map_view", :class => "toggle_map_button"} %> {:controller => :biogas_calculator, :action => :switch_view, :view => "charts"}, :method => :get, :loading => visual_effect(:blind_up, "biomass_configuration", :duration => 0.5, :queue => 'front'), :html => {:id => "chart_view", :class => "toggle_map_button"} %> 

当我点击地图标签时,我正在运行这个:

在HTML地图视图中:

  { :id => "configuration_map", :last_map => false }, :markers => { "data" => @json, "options" => {"list_container" => "markers_list" }}) %> 

在我的RJS(switch_view.js.rjs)中,biomass_configuration是HTML呈现表格,地图或图表的DIV:

 page.replace_html("biomass_configuration", :partial => "biomass_configuration") page[:biomass_configuration].visual_effect :blind_down, :duration => 1 if session[:view] == "map" page.delay 3 do page << "Gmaps.loadMaps();" <=== I thought this would do the trick end end 

当我单击选项卡时,它会正确地向上和向下显示表格视图和图表,但它仍然无法用于我的地图。

我想我应该在失明之后和我打电话给Gmaps.loadMaps()之前放一个延迟……我试着没有任何机会玩那个延迟。

如果我重新加载页面Ctrl + R,那么盲人上下似乎工作正常。

我也试过设置:last_map => true但没有成功…

这里的应用程序http://biowattsonline.com

我的application.js:

 //= require prototype //= require prototype_ujs //= require effects //= require dragdrop //= require controls //= require_tree . //= require gmaps4rails/bing.js //= require gmaps4rails/googlemaps.js //= require gmaps4rails/mapquest.js //= require gmaps4rails/openlayers.js //= require gmaps4rails/all_apis.js //= require facebox.js //= require i18n //= require i18n/translations 

20121229 – 解决问题的新方法:

我没有替换div bio_configuration,而是决定加载所有3个视图,只显示选项卡上的一个视图。 所以其他DIV都设置为display:none。

我更改了标签按钮link_to_remote和我的switch_view.rjs:

我的TAB按钮:

 {:controller => :biogas_calculator, :action => :switch_view, :view => "table"}, :method => :get, :loading => visual_effect(:fade, "biomass_configuration_charts", :duration => 1, :queue => 'front') + visual_effect(:fade, "biomass_configuration_map", :duration => 1, :queue => 'front'), :html => {:id => "table_view", :class => "toggle_map_button"} %> {:controller => :biogas_calculator, :action => :switch_view, :view => "map"}, :method => :get, :before => visual_effect(:fade, "biomass_configuration_charts", :duration => 1, :queue => 'front') + visual_effect(:fade, "biomass_configuration_table", :duration => 1, :queue => 'front'), :loading => visual_effect(:appear, "biomass_configuration_map", :duration => 1, :queue => 'end'), :complete => 'Gmaps.loadMaps();' , :html => {:id => "map_view", :class => "toggle_map_button"} if logged_in? %> {:controller => :biogas_calculator, :action => :switch_view, :view => "charts"}, :method => :get, :loading => visual_effect(:fade, "biomass_configuration_table", :duration => 1, :queue => 'front') + visual_effect(:fade, "biomass_configuration_map", :duration => 1, :queue => 'front'), :html => {:id => "chart_view", :class => "toggle_map_button"} %> 

和我的RJS:

 if session[:view] == "map" #page[:biomass_configuration_map].visual_effect :appear end if session[:view] == "table" page[:biomass_configuration_table].visual_effect :appear end if session[:view] == "charts" page[:biomass_configuration_charts].visual_effect :appear end 

这次地图显示但是在一个角落里。 我之前遇到的一个问题,但不知何故,修复程序对那个问题没有起作用。

20120101 – 添加了Goggle地图API库

在我的布局中:

         

在application.js中的其他库之前加载Google maps api

仍然适用于我的开发环境,但不能在Heroku上部署。

20120103更新

HEADER页面生成

              

FOOTER生成

       Gmaps.configuration_map = new Gmaps4RailsGoogle(); Gmaps.load_configuration_map = function() { Gmaps.configuration_map.map_options.detect_location = false; Gmaps.configuration_map.map_options.center_on_user = false; Gmaps.configuration_map.map_options.zoom = 0; Gmaps.configuration_map.map_options.id = 'configuration_map'; Gmaps.configuration_map.map_options.last_map = false; Gmaps.configuration_map.initialize(); Gmaps.configuration_map.markers = [{"description": "

Dec 13,2010 16:32 UTC

SELLING

Lucerne gras, 2. cut, start floweringcancelled

2000 tons in Goma, Republique democratique du congo

Posted by biowatts
", "sidebar": "Lucerne gras, 2. cut, start flowering
2000 t/a
", "lng": "29.225241", "lat": "-1.693314"},{"description": "

Sep 17, 2011 23:43 UTC

WANTED

feeder cattle liquid manurecancelled

1000 tons in Rio de janeiro, Brasil

Posted by biowatts
", "sidebar": "feeder cattle liquid manure
1000 t/a
", "lng":"-43.2095869", "lat": "-22.9035393"},{"description": "

Sep 17, 2011 23:43 UTC

WANTED

Dummycancelled

4000 tons in Sydney, Australia

Posted by biowatts
", "sidebar": "Dummy
4000 t/a
", "lng": "151.2070914", "lat": "-33.8689009"}]; Gmaps.configuration_map.markers_conf.list_container = 'markers_list'; Gmaps.configuration_map.create_markers(); Gmaps.configuration_map.adjustMapToBounds(); Gmaps.configuration_map.callback(); }; window.onload = function() { Gmaps.loadMaps(); }; false

注意Gmaps.loadMaps尽管:last_option => false …我认为只有在:last_map选项设置为true时才会发生?

我之前遇到过这种情况,它与gmaps4rails

你必须:

  • 手动包含页面中的所有js文件,以便在加载页面时正确加载它们

  • 将正确的选项传递给帮助程序,以便它不会再尝试包含它

  • 如您Gmaps.loadMaps()使用Gmaps.loadMaps()

总而言之,这只是js文件加载的问题。


让我们说清楚:

要包含在html中的文件(我假设您使用谷歌地图):

   

要包含在js清单中的文件:

 //= require ./gmaps4rails/googlemaps.js 

正如您所做的那样,必须在application.js文件之前添加google maps javascript。

要确保gmaps4rails文件包含在您的应用程序中,您可以运行生成器将它们复制到您的应用程序中: rails generate gmaps4rails:install 。 从那里,您可以删除大部分文件,具体取决于您使用的地图API。


以这种方式将您的电话转接给帮助者:

 <%= gmaps(:map_options => { :id => "configuration_map" }, :markers => { "data" => @json, "options" => {"list_container" => "markers_list" }}, :scripts => :none, :last_map => false) %> 

边注:

你加载js文件http://maps.gstatic.com两次,但有不同的选项,你可以简单地做: