3

Gmaps4rails 未在 rails 4.0.0 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 is not defined ” Ruby 2.0.0 rails 4.0.0 gmaps4rails 2.0.3

有什么建议请回复......

4

1 回答 1

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 %>
于 2014-02-17T07:21:35.510 回答