Rails 4 - Gmaps4Rails - 地图不会渲染 [英] Rails 4 - Gmaps4Rails - map won't render

查看:181
本文介绍了Rails 4 - Gmaps4Rails - 地图不会渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这一切工作正常 - 我继续处理下一个功能,并再次找到它不再有效。



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



模型之间的关联是:

地址

  belongs_to:addressable,:polymorphic => true 

简介

  has_many:地址,如::addressable 
accepted_nested_attributes_for:addresses,reject_if :: all_blank,allow_destroy:true



$ p
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $#$> $地址$,$:$地址,$地址$ B $ accept_nested_attributes_for:addresses,reject_if ::all_blank,allow_destroy:true

控制器显示功能为:

地址

  def create 
@address = Address.new(address_params)
授权@地址

respond_to do | format |
if @ address.save
format.html {redirect_to @address,notice:'地址已成功创建'}
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 = Organization .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(@地址)do | address,marker |
marker.lat address.latitude
marker.lng address.longitude
marker.infowindow address.full_address

end



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

  <%=呈现部分:profiles / main_address%> 

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

 < script src =// maps.google.com/maps/api/js?v=3.18&sensor=false&client=&key=&libraries =几何&安培;语言=安培; HL =&安培;区域=>< /脚本> 
将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'> ;< /脚本> <! - 仅当您需要自定义信息框 - >

< div style ='width:800px;'>
< div id =mapstyle ='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',要求:'google / api_client'
gem'gmaps4rails','〜> 2.1','> = 2.1.2'
gem'下划线'

我无法弄清楚为什么我只是得到一个空白区域而不是显示地址的地图。



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

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

使用谷歌搜索错误,Uncaught ReferenceError:MarkerClusterer未定义,宝石制造商建议运行:

  rails生成gmaps4rails:安装

当我尝试时,出现一个错误消息:

 在过程9339中通过Spring preloader运行0 
无法找到生成器'gmaps4rails:install'。也许你的意思是'paper_trail:install','gmaps4rails:copy_js'或'responders:install'

我的控制台检查器还会显示一些与gmaps相关的警告,如:

  Google Maps API警告:NoApiKeys https://开发人员。 google.com/maps/documentation/javascript/error-messages#no-api-keys 
util.js:220 Google Maps API警告:RetiredVersion https://developers.google.com/maps/documentation/javascript/ error-messages#retired-version
util.js:220 Google Maps API警告:SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required
util.js:220 Google Maps API警告:InvalidClientId https://developers.google.com/maps/documentation/javascript/error-messages#invalid-client-id
util.js:220 Google Maps API警告:InvalidKey https://developers.google.com/maps/documentation/javascript/error-messages#invalid-key
14:1无法解码下载的字体:http://本地主机:3000 /资产/ flaticon-080b09d3f53cb13c2f9d9a4c53ad7a71206bd8e2390c3e18d2b42ce9388a49a6.woff



我有一个API密钥 - I只是不不知道把它放在我的代码中。



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

解决方案

所以你的问题是这两行:

 < 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'> ;< /脚本>

有人引用 markerclusterer infobox 直接来自Google SVN的库(这是一个糟糕的做法),并且该库在几周前转移到了Github。

您可以从信息框库rel = nofollow noreferrer>这里给你的项目和参考 infobox_packed.js 文件:

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

同样,您可以下载标记集群库这里。小心:对于标记集群,您将需要 src 图片



请阅读此 SO问题与答案,也<一个href =https://stackoverflow.com/questions/37469525/add-custom-image-to-marker-cluster-without-overwriting-markers>这一个也许这是一个。我强烈建议您阅读所有问题的答案,因为其中一些答案包含不好的解决方案(来自新github位置的参考库,而不是将它们下载到您的项目中,并且从那里引用它们是不好的做法)。

b $ b

I'm struggling to get my google maps to work in my Rails app.

It was all working fine - I moved on to work on the next feature and have come back to find it no longer works.

I have models for address, profile and project. I use address in each of profile and project In each case, the map does not display.

The associations between models are:

Address

belongs_to :addressable, :polymorphic => true

Profile

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

Project

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

The controller show functions are:

Address

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

Profile

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

Project

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

In my profiles show, I have:

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

In profiles/main address, I have:

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

I have the same process in my projects folder for showing the project address.

In my gem file I have:

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

I can't figure out why I just get a blank space instead of a map showing the address.

In my console inspector, I can see a red error that says:

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

Googling the error, Uncaught ReferenceError: MarkerClusterer is not defined, the gem maker suggests running:

rails generate gmaps4rails:install

When I try that, I get an error that says:

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'

My console inspector also shows a number of warnings related to gmaps, as:

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

I have an api key - I just don't know where to put it in my code.

Can anyone see how to help. I don't mind if I don't solve the warning problems with Gmaps, but I do want to get back to where I was when the map displayed.

解决方案

So your problem is with these two lines:

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

Somebody referenced markerclusterer and infobox libraries directly from Google SVN (which is a bad practise) and the library was moved to Github couple of weeks ago.

You can download the infobox library from here to your project and reference infobox_packed.js file:

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

Similarly, you can download the marker clusterer library here. Careful: for marker cluster you will need both src and images.

Please read this SO question and answers, also this one and maybe this one. I strongly advice you to read all the answers in the questions, as some of them contain bad solutions (reference libraries from the new github location, instead of downloading them to your project and referencing them from there is bad practise)

这篇关于Rails 4 - Gmaps4Rails - 地图不会渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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