【问题标题】:Rails 4 - Gmaps4Rails - map won't renderRails 4 - Gmaps4Rails - 地图不会渲染
【发布时间】:2016-06-01 02:20:10
【问题描述】:

我正在努力让我的谷歌地图在我的 Rails 应用程序中工作。

一切正常 - 我继续开发下一个功能,但回来发现它不再有效。

我有地址、个人资料和项目的模型。我在每个配置文件和项目中使用地址在每种情况下,地图都不会显示。

模型之间的关联是:

地址

belongs_to :addressable, :polymorphic => true

简介

has_many :addresses, as: :addressable
    accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true

项目

has_many :addresses, as: :addressable
    accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true

控制器显示功能有:

地址

def create
    @address = Address.new(address_params)
    authorize @address

    respond_to do |format|
      if @address.save
        format.html { redirect_to @address, notice: 'Address was successfully created.' }
        format.json { render :show, status: :created, location: @address }
      else
        format.html { render :new }
        format.json { render json: @address.errors, status: :unprocessable_entity }
      end
    end
  end

简介

def show
    # debugger
    @profile = Profile.includes(:industries).find(params[:id])
    # @organisation = Organisation.find(params[:organisation_id])
    # @profiles = @organisation.profiles
    @addresses = @profile.addresses

      @hash = Gmaps4rails.build_markers(@addresses) do |address, marker|
        marker.lat address.latitude
        marker.lng address.longitude
        marker.infowindow address.full_address
      end
  end

项目

def show
    @invite = Invite.new
    @project = Project.find(params[:id])
    @addresses = @project.addresses

      @hash = Gmaps4rails.build_markers(@addresses) do |address, marker|
        marker.lat address.latitude
        marker.lng address.longitude
        marker.infowindow address.full_address

      end

在我的个人资料显示中,我有:

  <%= render partial: "profiles/main_address" %>

在个人资料/主要地址中,我有:

<script src="//maps.google.com/maps/api/js?v=3.18&sensor=false&client=&key=&libraries=geometry&language=&hl=&region="></script> 
<script src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox_packed.js' type='text/javascript'></script> <!-- only if you need custom infoboxes -->

<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>

<script>
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers(<%=raw @hash.to_json %>);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
  handler.getMap().setZoom(8);
});
</script>

我的项目文件夹中显示项目地址的过程相同。

在我的 gem 文件中,我有:

gem 'google-api-client', '~> 0.7.1', require: 'google/api_client'
gem 'gmaps4rails', '~> 2.1', '>= 2.1.2'
gem 'underscore-rails'

我不明白为什么我只得到一个空白而不是显示地址的地图。

在我的控制台检查器中,我可以看到一个红色错误:

primitives.self-5b8a3a6….js?body=1:16 Uncaught ReferenceError: MarkerClusterer is not definedGmaps.Google.Primitives @ primitives.self-5b8a3a6….js?body=1:16Gmaps.Objects.Handler.Handler.setPrimitives @ handler.self-2f220ca….js?body=1:122Handler @ handler.self-2f220ca….js?body=1:8Gmaps.build @ base.self-8dd1d1a….js?body=1:9(anonymous function) @ 14:760

谷歌搜索错误,Uncaught ReferenceError: MarkerClusterer is not defined,gem maker建议运行:

rails generate gmaps4rails:install

当我尝试这样做时,我收到一条错误消息:

Running via Spring preloader in process 93390
Could not find generator 'gmaps4rails:install'. Maybe you meant 'paper_trail:install', 'gmaps4rails:copy_js' or 'responders:install'

我的控制台检查器还显示了许多与 gmap 相关的警告,例如:

Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys
util.js:220 Google Maps API warning: RetiredVersion https://developers.google.com/maps/documentation/javascript/error-messages#retired-version
util.js:220 Google Maps API warning: SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required
util.js:220 Google Maps API warning: InvalidClientId https://developers.google.com/maps/documentation/javascript/error-messages#invalid-client-id
util.js:220 Google Maps API warning: InvalidKey https://developers.google.com/maps/documentation/javascript/error-messages#invalid-key
14:1 Failed to decode downloaded font: http://localhost:3000/assets/flaticon-080b09d3f53cb13c2f9d9a4c53ad7a71206bd8e2390c3e18d2b42ce9388a49a6.woff

我有一个 api 密钥 - 我只是不知道将它放在我的代码中的什么位置。

任何人都可以看到如何提供帮助。如果我不解决 Gmaps 的警告问题,我不介意,但我确实想回到地图显示时的位置。

【问题讨论】:

    标签: javascript ruby-on-rails ruby google-maps


    【解决方案1】:

    所以你的问题在于这两行:

    <script src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js"></script>
    <script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox_packed.js' type='text/javascript'></script>
    

    有人直接从 Google SVN 引用了 markerclusterer 和 infobox 库(这是一种不好的做法),几周前该库已移至 Github。

    您可以从here 下载infobox 库到您的项目并引用infobox_packed.js 文件:

    <script src='path/to/infobox_packed.js' type='text/javascript'></script>
    

    同样,您可以下载标记聚类库here。小心:对于标记集群,您将同时需要 src 和 images。

    请阅读SO question and answers、this one 和 this one。我强烈建议您阅读问题中的所有答案,因为其中一些包含错误的解决方案(从新的 github 位置引用库,而不是将它们下载到您的项目并从那里引用它们是不好的做法)

    【讨论】:

    • 嗨 Matej,谢谢。您的意思是克隆存储库并在项目中引用它们是不好的做法,还是引用 github 文件是不好的做法。其他帖子建议依赖 github 参考的不良做法。
    • 是的。你不应该从 github 引用。克隆 repo 并在里面引用它们是绝对可以的。这是我链接其他答案的主要原因,我很高兴你明白我的意思:)
    猜你喜欢
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    相关资源
    最近更新 更多