在rails 4 + gmaps4rails 2.0.3中未定义Gmaps

时间:2013-11-11 12:06:24

标签: ruby-on-rails-4 gmaps4rails2

在rails 4.0.0中未定义Gmaps4rails Google Maps for Rails 我正在按照本教程“http://rubydoc.info/gems/gmaps4rails/2.0.4/frames

1) Gemfile

gem 'gmaps4rails'

2) HTML on view page   
<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>

3) on view page    
<script src="//maps.google.com/maps/api/js?v=3.13&amp;sensor=false&amp;libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>
You'll require underscore.js too, see here: underscorejs.org/

3) Javascript source code

If you have the asset pipeline, add this:

//= require underscore
//= require gmaps/google
If you don't have asset pipeline, you'll need to import the js OR coffee files:

rails g gmaps4rails:copy_js

rails g gmaps4rails:copy_coffee
4) Javascript code:

Create your map:

handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers([
    {
      "lat": 0,
      "lng": 0,
      "picture": {
        "url": "https://addons.cdn.mozilla.net/img/uploads/addon_icons/13/13028-64.png",
        "width":  36,
        "height": 36
      },
      "infowindow": "hello!"
    }
  ]);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
});

但当我在萤火虫检查时,显示“ Gmaps未定义” Ruby 2.0.0 rails 4.0.0 gmaps4rails 2.0.3

任何建议请回复.....

1 个答案:

答案 0 :(得分:5)

我遇到了这个问题。问题是我在加载核心gmaps javascript之前在视图中加载地图构建javascript。

您可以通过在javascripts之后添加yield来解决此问题,并使用content_for块将地图构建javascript放在其他地方之后。像这样:

<script src="//maps.google.com/maps/api/js?v=3.13&amp;sensor=false&amp;libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>    
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>  
<%= yield :javascripts %>

然后,无论您在哪里制作地图,

<% content_for :javascripts do %>
<script type='text/javascript'>
    handler = Gmaps.build('Google');
    /* rest of maps building JS */
</script>
<% end %>