Gmaps is not defined in rails 4 + gmaps4rails 2.0.3

我们两清 提交于 2019-12-01 23:50:00

问题


Gmaps4rails is not define in rails 4.0.0 Google Maps for Rails i am following this tutorial "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();
});

But when i am checking at firebug its showing "Gmaps is not defined" Ruby 2.0.0 rails 4.0.0 gmaps4rails 2.0.3

Any suggestion please reply.....


回答1:


I ran into this issue. The problem was that I was loading the map building javascript in the view before I was loading the core gmaps javascript.

You can solve this by add a yield after your javascripts, and using a content_for block to place your map building javascript after the others. Something like this:

<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 %>

and then, wherever you're building your map,

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


来源:https://stackoverflow.com/questions/19905972/gmaps-is-not-defined-in-rails-4-gmaps4rails-2-0-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!