【问题标题】:Rails, gmaps4rails don't work with httpsRails,gmaps4rails 不适用于 https
【发布时间】:2011-09-15 09:56:12
【问题描述】:

我目前正在使用 Rails 3.0.9 和 gmaps4rails 0.8.6。

我在为谷歌地图启用 https 时遇到问题。

我尝试使用该解决方案:

Using gmaps4rails with https/SSL

但没有成功。

在布局应用程序中,我添加了这个 js 包含标签:

<%= javascript_include_tag "https://maps.google.com/maps/api/js?sensor=false&libraries=geometry" %>
<%= javascript_include_tag "https://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js" %>
<%= javascript_include_tag "gmaps4rails.js" %>
<%= javascript_include_tag "https://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/src/markerclusterer_compiled.js" %>

我的控制器代码

...
@places = Place.paginate(:page => params[:page], :per_page => 10)
@json = @places.to_gmaps4rails

查看

...
 <div id="mapLocal"> <%= gmaps4rails(@json, true false) %> </div>
...

之后,谷歌地图仍然可以使用 http。

页面源代码

function gmaps4rails_loadScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.google.com/maps/api/js?sensor=false&libraries=geometry&callback=gmaps4rails_init";
  document.body.appendChild(script);
}

我尝试将 gmaps4rails 更改为 gmaps。

结果:

Completed 500 Internal Server Error in 54ms


ActionView::Template::Error (undefined method `to_gmaps4rails' for #<String:0xf5d98528>):
    13: <script type="text/javascript" charset="utf-8">
    14: function gmaps4rails_init() {
    15:     <% #true is passed here to trigger map initialization %>
    16:     <%=raw options.to_gmaps4rails(true) %>
    17: }
    18:   
    19: function gmaps4rails_loadScript() {
  app/views/places/index.html.erb:4:in `_app_views_places_index_html_erb___671263203__86055108_0'

所以我决定升级gem(可能老版本不支持ssl?)。

从 0.8.6 升级到 1.1.4 后,我的基本未更改代码甚至都不会生成地图

  <div id="mapLocal"> <%= gmaps4rails(@json) %> </div>

我用firebug检查了js日志,它返回了

Gmaps4RailsGoogle is not defined
Gmaps.map = new Gmaps4RailsGoogle(); 

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 google-maps gmaps4rails


    【解决方案1】:

    您应该升级到最新版本的 gem:Gmaps4RailsGoogle 在 0.x.x 中不存在。

    使用 1.x.x,您需要一个额外的文件:

    gmaps4rails.googlemaps.js
    

    “gmaps4rails.js”现在是“gmaps4rails.base.js”

    使用安装程序,您将复制所有内容。

    使用 gmaps 助手时,您需要提供一个哈希作为第一个参数:

    gmaps( :markers => { :data => @json } )
    

    在编码 1.0.0 时在必要时进行了一些更改,因为现在可以在同一页面上创建多个地图。无论如何,所有文档都在 wiki 中。 我想所有这些迹象都能满足您的需求。

    【讨论】:

    • 谢谢,现在标记可以工作了。这条线&lt;%= gmaps({"markers" =&gt; { "data" =&gt; @json }}, true, false) %&gt; 使谷歌地图工作得很好。 :D
    猜你喜欢
    • 2017-08-31
    • 2013-09-24
    • 2011-04-14
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    相关资源
    最近更新 更多