2016-06-01 64 views
0

我努力讓我的谷歌地圖在我的Rails應用程序中工作。Rails 4 - Gmaps4Rails - 地圖不會渲染

這一切工作正常 - 我繼續工作下一個功能,並已回來找到它不再有效。

我有模型地址,配置文件和項目。我在每個配置文件和項目中使用地址在每種情況下,地圖都不顯示。

模型之間的關聯是:

地址

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

谷歌搜索的錯誤,未捕獲的ReferenceError:未定義MarkerClusterer,創業板製造商建議在運行:

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' 

我的控制檯檢查器也顯示了許多有關gmaps警告,如:

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的警告問題,我不介意,但我確實想回到地圖顯示時的位置。

回答

0

所以你的問題是與這兩條線:

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

有人引用markerclustererinfobox庫直接從谷歌SVN(這是一個不好的做法)和庫前被轉移到Github上幾個星期。

您可以從here下載infobox庫到您的項目,並引用infobox_packed.js文件:

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

同樣的,你也可以下載標記聚類器庫here。小心:對於標記羣集,您將需要srcimages

請看這SO question and answers,也this one和也許this one。我強烈建議你閱讀所有問題的答案,因爲其中一些答案包含不好的解決方案(來自新的github位置的參考庫,而不是將它們下載到你的項目,並從那裏引用它們是不好的做法)

+0

嗨Matej ,謝謝你。你的意思是克隆回購並在項目中引用它們的不良做法,或者引用github文件是不好的做法。其他帖子表明不好的做法依賴於github參考。 – Mel

+0

是的。你不應該從github引用。克隆回購和引用它們是絕對沒問題的。這是我連接其他答案的主要原因,我很高興你明白我的意思:) –