Gmaps не определен в рельсах 4 + gmaps4rails 2.0.3
Gmaps4rails не определен в rails 4.0.0. Карты Google для 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&sensor=false&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();
});
Но когда я проверяю в firebug его показ "Gmaps не определен", Ruby 2.0.0 rails 4.0.0 gmaps4rails 2.0.3
Любое предложение, пожалуйста, ответьте.....
1 ответ
Я столкнулся с этим вопросом. Проблема заключалась в том, что я загружал javascript для построения карты в виде до того, как загружал основной gmaps javascript.
Вы можете решить эту проблему, добавив выход после ваших javascripts и используя блок content_for, чтобы разместить свой javascript для построения карты после других. Что-то вроде этого:
<script src="//maps.google.com/maps/api/js?v=3.13&sensor=false&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 %>