gmaps 未在 rails 4 + gmaps4rails 2.0.3 中定义 [英] Gmaps is not defined in rails 4 + gmaps4rails 2.0.3

查看:44
本文介绍了gmaps 未在 rails 4 + gmaps4rails 2.0.3 中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gmaps4rails 在 rails 4.0.0 中没有定义铁路谷歌地图我正在关注本教程http://rubydoc.info/gems/gmaps4rails/2.0.4/frames"

1) Gemfile宝石'gmaps4rails'2) 视图页面上的 HTML<div style='width: 800px;'><div id="map" style='width: 800px;高度:400px;'></div>

3)在查看页面<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'></脚本>您也需要 underscore.js,请参见此处:underscorejs.org/3) Javascript 源代码如果您有资产管道,请添加以下内容://= 需要下划线//= 需要 gmaps/google如果您没有资产管道,则需要导入 js 或 coffee 文件:rails g gmaps4rails:copy_jsrails g gmaps4rails:copy_coffee4)Javascript代码:创建您的地图:handler = Gmaps.build('Google');handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){标记 = handler.addMarkers([{纬度":0,"lng": 0,图片": {"url": "https://addons.cdn.mozilla.net/img/uploads/addon_icons/13/13028-64.png",宽度":36,高度":36},信息窗口":你好!"}]);handler.bounds.extendWith(markers);handler.fitMapToBounds();});

但是当我检查萤火虫时,它显示Gmaps 未定义"红宝石 2.0.0导轨 4.0.0gmaps4rails 2.0.3

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

解决方案

我遇到了这个问题.问题是我在加载核心 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 src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></脚本><%= javascript_include_tag "application", "data-turbolinks-track" =>真%><%=产量:javascripts%>

然后,无论您在何处构建地图,

<% content_for :javascripts do %><script type='text/javascript'>handler = Gmaps.build('Google');/* 其余的地图构建 JS */<%结束%>

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

解决方案

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

这篇关于gmaps 未在 rails 4 + gmaps4rails 2.0.3 中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆