【问题标题】:GMaps don't load at first open page, after reload it works地图在第一次打开页面时不加载,重新加载后它可以工作
【发布时间】:2015-10-04 23:41:48
【问题描述】:

我使用 gmaps4rails gem,地图并不总是在打开第一页时加载,重新加载后它可以工作。

所以,我的代码 show.html.haml

    %script{:src => "//maps.google.com/maps/api/js?v=3.13&sensor=false&libraries=geometry", :type => "text/javascript"}
    %script{:src => "//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js", :type => "text/javascript"}

........

    .col-lg-8.col-sm-8.col-md-8.col-xs-12
        #map
        %br/

......

    :javascript
        handler = Gmaps.build('Google');
        handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
            markers = handler.addMarkers(#{raw @hash.to_json});
            handler.bounds.extendWith(markers);
            handler.fitMapToBounds();
            handler.getMap().setZoom(16);
        });

在 parks_controller.rb

中
  def show
    set_park
    @hash = Gmaps4rails.build_markers(@park) do |park, marker|
      marker.lat park.latitude
      marker.lng park.longitude
      marker.infowindow park.address
    end

在index.html.haml

中
 - @parks.each do |park|
    .map_index
      .image_wrapper
        = image_tag "http://maps.google.com/maps/api/staticmap?size=400x200&sensor=false&zoom=16&markers=#{park.latitude}%2C#{park.longitude}", class: "map_image"
      %h2
        = link_to park.title, park

工作应用 - https://geoparks.herokuapp.com/

请告诉我,要更改或添加到代码中的哪些内容?

【问题讨论】:

    标签: javascript ruby-on-rails ruby google-maps gmaps4rails


    【解决方案1】:

    我只是添加到指向地图页面参数的链接

    'data-no-turbolink' => true
    

    代码= link_to p.title, parkin index.html.haml改为

    = link_to park.title, park, 'data-no-turbolink' => true
    

    【讨论】:

    • 我不知道 turbolinks 已经给我带来了多少痛苦。它带来的小用处根本没有理由将它保留在应用程序中。
    猜你喜欢
    • 2016-11-23
    • 2014-11-12
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多