多年来,我一直在尝试将gmaps4rails整合到Rails应用程序中。
我已经创建了一个全新的应用程序并进行了尝试。
我无法弄清楚出了什么问题。我正在寻找完整且最新的设置说明。许多SO帖子引用了旧版本的依赖关系,最终说该问题已在较新版本中解决。
目前,我在我的视图中有这个:
<script src="//maps.google.com/maps/api/js?v=3.23&key=<%= ENV['GOOGLE_MAPS_API_KEY'] %>"></script>
<script src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js" type="text/javascript"></script>
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
在我的application.js文件中,我有以下代码:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require moment
//= require bootstrap-datetimepicker
//= require pickers
//= require underscore
//= require gmaps/google
//= require markerclusterer
//= require_tree .
我已经将underscore.js生产版本保存在我的vendor/assets/javascripts文件夹中。我也将markerclusterer.js保存为同一文件夹中的一个文件。
在我的gem文件中,我有:
gem 'geocoder'
gem 'gmaps4rails', '~> 2.1', '>= 2.1.2'
gem 'countries'
gem 'country_select'
在我的控制器中,我有以下代码:
class AddressesController < ApplicationController
def index
end
def show
@hash = Gmaps4rails.build_markers(@address) do |address, marker|
marker.lat address.latitude
marker.lng address.longitude
end
end
def new
@address = Address.new
end
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
private
# Use callbacks to share common setup or constraints between actions.
def set_address
@address = Address.find(params[:id])
# authorize @address
end
# Never trust parameters from the scary internet, only allow the white list through.
def address_params
params[:address].permit(:unit, :building, :street_number, :street, :city, :region, :zip, :country, :time_zone, :latitude, :longitude)
end
def user_time_zone(&block)
Time.use_zone(current_user.time_zone, &block)
end
end
当我尝试使用地图渲染视图时,Chrome检查器显示以下错误:
v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js 资源加载失败:服务器响应404(未找到) chrome-extension://mkjojgglmmcghgaiknnpgjgldgaocjfd/content/contentScripts/kwift.CHROME.min.js:1271 Uncaught SyntaxError: 标识符“findGoodContent”已经声明 util.js:221 Google Maps API警告:NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys util.js:221 Google Maps API警告:RetiredVersion https://developers.google.com/maps/documentation/javascript/error-messages#retired-version util.js:221 Google Maps API警告:InvalidKey https://developers.google.com/maps/documentation/javascript/error-messages#invalid-key
我不理解这些错误,也找不到如何在Rails中设置此gem的说明。我可以看到它已被下载很多次 - 人们一定已经弄清楚了如何设置它。我试图弄清楚这个问题,让自己变得疯狂。
重新生成API密钥后
我尝试在Google控制台上重新生成我的浏览器API密钥。
现在当我尝试渲染页面时,我得到以下控制台错误:
primitives.self-5b8a3a6….js?body=1:5 Uncaught ReferenceError: google is not defined(…)Gmaps.Google.Primitives @ primitives.self-5b8a3a6….js?body=1:5Gmaps.Objects.Handler.Handler.setPrimitives @ handler.self-2f220ca….js?body=1:122Handler @ handler.self-2f220ca….js?body=1:8build @ base.self-8dd1d1a….js?body=1:9(anonymous function) @ VM2063:2t.SnapshotRenderer.n.assignNewBody @ turbolinks.self-c5acd7a….js?body=1:6t.SnapshotRenderer.n.replaceBody @ turbolinks.self-c5acd7a….js?body=1:6(anonymous function) @ turbolinks.self-c5acd7a….js?body=1:6t.Renderer.t.renderView @ turbolinks.self-c5acd7a….js?body=1:6t.SnapshotRenderer.n.render @ turbolinks.self-c5acd7a….js?body=1:6t.Renderer.t.render @ turbolinks.self-c5acd7a….js?body=1:6t.View.e.renderSnapshot @ turbolinks.self-c5acd7a….js?body=1:6t.View.e.render @ turbolinks.self-c5acd7a….js?body=1:6t.Controller.r.render @ turbolinks.self-c5acd7a….js?body=1:6(anonymous function) @ turbolinks.self-c5acd7a….js?body=1:6(anonymous function) @ turbolinks.self-c5acd7a….js?body=1:6
turbolinks.self-c5acd7a….js?body=1:6 GET http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js t.SnapshotRenderer.n.assignNewBody @ turbolinks.self-c5acd7a….js?body=1:6t.SnapshotRenderer.n.replaceBody @ turbolinks.self-c5acd7a….js?body=1:6(anonymous function) @ turbolinks.self-c5acd7a….js?body=1:6t.Renderer.t.renderView @ turbolinks.self-c5acd7a….js?body=1:6t.SnapshotRenderer.n.render @ turbolinks.self-c5acd7a….js?body=1:6t.Renderer.t.render @ turbolinks.self-c5acd7a….js?body=1:6t.View.e.renderSnapshot @ turbolinks.self-c5acd7a….js?body=1:6t.View.e.render @ turbolinks.self-c5acd7a….js?body=1:6t.Controller.r.render @ turbolinks.self-c5acd7a….js?body=1:6(anonymous function) @ turbolinks.self-c5acd7a….js?body=1:6(anonymous function) @ turbolinks.self-c5acd7a….js?body=1:6
turbolinks.self-c5acd7a….js?body=1:6 GET http://localhost:3000/users/assets/images/grayscale.svg t.SnapshotRenderer.n.assignNewBody @ turbolinks.self-c5acd7a….js?body=1:6t.SnapshotRenderer.n.replaceBody @ turbolinks.self-c5acd7a….js?body=1:6(anonymous function) @ turbolinks.self-c5acd7a….js?body=1:6t.Renderer.t.renderView @ turbolinks.self-c5acd7a….js?body=1:6t.SnapshotRenderer.n.render @ turbolinks.self-c5acd7a….js?body=1:6t.Renderer.t.render @ turbolinks.self-c5acd7a….js?body=1:6t.View.e.renderSnapshot @ turbolinks.self-c5acd7a….js?body=1:6t.View.e.render @ turbolinks.self-c5acd7a….js?body=1:6t.Controller.r.render @ turbolinks.self-c5acd7a….js?body=1:6(anonymous function) @ turbolinks.self-c5acd7a….js?body=1:6(anonymous function) @ turbolinks.self-c5acd7a….js?body=1:6
util.js:221 Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keysuD.S @ util.js:221(anonymous function) @ js?v=3.23&key=:127(anonymous function) @ js?v=3.23&key=:47(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:47(anonymous function) @ js?v=3.23&key=:97(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:97(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:97(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:97jc @ js?v=3.23&key=:46gc.Qc @ js?v=3.23&key=:97(anonymous function) @ common.js:1
util.js:221 Google Maps API warning: RetiredVersion https://developers.google.com/maps/documentation/javascript/error-messages#retired-versionuD.S @ util.js:221(anonymous function) @ js?v=3.23&key=:127(anonymous function) @ js?v=3.23&key=:47(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:47(anonymous function) @ js?v=3.23&key=:97(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:97(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:97(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:97jc @ js?v=3.23&key=:46gc.Qc @ js?v=3.23&key=:97(anonymous function) @ common.js:1
util.js:221 Google Maps API warning: InvalidKey https://developers.google.com/maps/documentation/javascript/error-messages#invalid-keyuD.S @ util.js:221(anonymous function) @ js?v=3.23&key=:127(anonymous function) @ js?v=3.23&key=:47(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:47(anonymous function) @ js?v=3.23&key=:97(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:97(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:97(anonymous function) @ js?v=3.23&key=:44(anonymous function) @ js?v=3.23&key=:97jc @ js?v=3.23&key=:46gc.Qc @ js?v=3.23&key=:97(anonymous function) @ common.js:1
我可以在终端中输入rails s命令以启动应用程序的服务器。
ActionController::RoutingError (No route matches [GET] "/assets/underscore-min.map"):
Parameters: {"id"=>"4"}
ActionController::RoutingError (No route matches [GET] "/users/assets/images/grayscale.svg"):
与下划线路由错误相关的问题,终端中的行指向assets/underscore。我的文件结构是vendor/assets/javascripts,然后将文件保存为underscore.js(而不是min.map)。 另一种尝试 我尝试用js v3.24替换v3.23,这样就消除了上面的警告信息。然而,我仍然无法渲染地址地图。当我检查Chrome控制台检查器时,我可以看到地址在JavaScript中被确认,但纬度和经度的详细信息没有显示:
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers([{"lat":null,"lng":null,"infowindow":"Unit 1\u003cbr\u003e34 Darling Street\u003cbr\u003eBuilding D\u003cbr\u003eBalmain East NSW 2041\u003cbr\u003eAustralia"}]);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
我现在想知道这个问题是否与模型关联有关。
我有一个地址模型和一个组织模型。它们的关联是:
地址:
belongs_to :addressable, :polymorphic => true, optional: true
组织:
has_many :addresses, as: :addressable
accepts_nested_attributes_for :addresses, reject_if: :all_blank, allow_destroy: true
在我的地址控制器展示操作中,我有以下内容:
def show
@hash = Gmaps4rails.build_markers(@address) do |address, marker|
marker.lat user.latitude
marker.lng user.longitude
# marker.title user.title
end
end
在我的组织控制器展示行为中,我有以下内容:
def show
@addresses = @organisation.addresses
@hash = Gmaps4rails.build_markers(@addresses) do |address, marker|
marker.lat address.latitude
marker.lng address.longitude
marker.infowindow address.full_address
end
end
带地图的页面部分在 views/addresses/map.html.erb 中。
当我尝试再次渲染页面时,出现了许多错误,所有这些错误都是这样说的:
js?v=3.24&key=AIzaSyAleQgfNH3HRQVUCYnyAzp46xmXW7WrWrc:37 Uncaught RangeError: Maximum call stack size exceeded
我阅读了其他SO帖子,其中人们出现了这个错误,并且注释继续讨论“递归循环”。我似乎没有任何类似于这些错误中显示的代码,因此我不确定如何处理这些消息。
我想知道是否需要更改组织控制器操作,以便某种方式引用组织地址而不是地址总体(尽管如果是这种情况,那么我不明白控制台检查器如何显示js中的图表局部正确标识的地址)。
有谁能够提供详细的分步教程,以便我可以尝试找到解决方法吗?
其他遇到相似问题的人
我可以从其他SO帖子中看到,其他人也遇到了类似的问题。建议似乎是这个答案: Gmaps4rails Maximum call stack size exceeded?
但我认为自己已经这样做了 - 每个地址控制器和组织控制器中的show操作都引用纬度/经度而不是完整单词(在数据库中使用)。
ERIC的建议
采纳Eric的建议,我从gem文件中删除了gmaps4rails gem,以及从application.js中删掉了require/gmaps行。
我将我的组织控制器的show操作更改为:
def show
@addresses = @organisation.addresses.all
# @hash = Gmaps4rails.build_markers(@addresses) do |address, marker|
# marker.lat address.latitude
# marker.lng address.longitude
# marker.infowindow address.full_address
# end
end
这一步与Eric在他的标记控制器的index行动中提出的建议相同。我有一个独立的地址控制器,它有一个类似于Eric的index行动。在我的数据库中,地址是多态的,并属于组织机构。我正在尝试在组织机构展示页面上呈现地图,因此我认为这一步与Eric的建议一致。
接下来,我使用以下内容更新了我的addresses/_map.html.erb文件:
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<%= javascript_tag do %>
var addresses = <%= raw @addresses.to_json %>;
<% end %>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=<%= ENV['GOOGLE_MAPS_API_KEY'] %>&callback=initMap">
</script>
我在我的app/javascripts文件夹中创建了一个名为addresses.js的新文件,其内容如下:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4
});
var bounds = new google.maps.LatLngBounds();
var n = addresses.length;
for (var i = 0; i < n; i++) {
var address = new google.maps.Address({
position: {lat: parseFloat(addresses[i].latitude), lng: parseFloat(addresses[i].longitude)},
title: addresses[i].name,
map: map
});
bounds.extend(address.position);
}
map.fitBounds(bounds);
}
当我保存所有内容并尝试时,屏幕上会出现一个空的灰色框。
当我使用Chrome检查器时,我可以看到:
//<![CDATA[
var addresses = [{"id":5,"unit":"1","building":"d","street_number":"34","street":"darling street","city":"Balmain East","region":"NSW","zip":"2041","country":"AU","time_zone":"International Date Line West","addressable_id":1,"addressable_type":"Organisation","description":"main_address","created_at":"2016-10-27T19:17:27.919Z","updated_at":"2016-11-08T22:48:16.978Z","latitude":"-33.85751","longitude":"151.193546"}];
//]]>
以上是数据库中的地址。但它没有生成地图。
有人能看出我需要做什么才能在地址上呈现地图吗?
当我查看Chrome控制台检查器时
我可以在控制台检查器中看到几个新的错误。
它们分别是:
addresses.self-176b72f….js?body=1:9 Uncaught TypeError: google.maps.Address is not a constructor
这篇文章有一个类似的问题。被接受的解决方案是添加回调函数。我不知道应该在哪里以及如何添加。