【发布时间】:2013-07-31 03:36:48
【问题描述】:
使用 Rails 3.2.13、ruby 1.9.3p374 和 Gmaps4rails 1.5.6。
我一直在努力解决一些问题,起初我认为应该相当直截了当:我想显示一个标记,当单击该标记时,它将重定向到特定路径,并在同一窗口中加载该路径。
Gmaps4rails 在他们的description 中没有简单的选择。所以我花了无数个小时在网上搜索,我在 StackOverflow 中找到了两个最好的点击:this 闻起来是正确的路径,但this one 似乎也很有用。
最后,我的控制器看起来像:
@gmapsjson = current_user.houses.all.to_gmaps4rails do | house, marker |
marker.title house.reference
marker.json({:link => polymorphic_url(house, :routing_type => :path)})
end
这是正确的(我认为)在处理后的 html 中生成以下内容:
<script src="//maps.google.com/maps/api/js?v=3.8&sensor=false&client=&key=&libraries=geometry&language=&hl=&region=" type="text/javascript"></script>
<script type="text/javascript">
Gmaps.map = new Gmaps4RailsGoogle();
Gmaps.load_map = function() {
Gmaps.map.map_options.auto_zoom = false;
Gmaps.map.map_options.zoom = 3;
Gmaps.map.initialize();
Gmaps.map.markers = [{"title":"First House","link":"/houses/1","lat":-3.4671425,"lng":12.5264373},{"title":"Second House","link":"/houses/2","lat":-4.5543296,"lng":-3.4151647}];
Gmaps.map.create_markers();
Gmaps.map.adjustMapToBounds();
Gmaps.map.callback();
};
Gmaps.oldOnload = window.onload;
window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };
</script>
标记出现在正确的位置。但是现在,解释first relevant reference 上的描述,我直接在视图中输入,确保它会出现在生成的 html 中所有其他 javascripts Gmaps4Rails 转储的下方:
<script type="text/javascript">
function redirect_to(url) {
window.location = url
};
Gmaps.callback = function() {
for (var i = 0; i < Gmaps.map.markers.length; ++i) {
google.maps.event.addListener(Gmaps.map.markers[i].google_object, 'click', redirect_to(Gmaps.map.markers[i].link));
}
};
</script>
但是现在,每当我加载页面时,我的 Firefox javascript 控制台都会显示:
TypeError: Gmaps[load_function_name] is not a function
for (key in Gmaps) {
value = Gmaps[key];
searchLoadIncluded = key.search(/load/);
if (searchLoadIncluded === -1) {
load_function_name = "load_" + key;
_results.push(Gmaps[load_function_name]()); <=== Points to this line
} else {
_results.push(void 0);
}
}
那么,有什么想法是错误的吗?
提前致谢,
马可
【问题讨论】:
-
你试过这个答案吗? stackoverflow.com/questions/8608602/…
-
我做到了,是的。它在原始问题中被称为“但这似乎也有用”。但感谢您提醒我注意这一点。 :) 这种方法在 javascript 控制台中给了我一个未知的“google”。
标签: ruby-on-rails ruby ruby-on-rails-3 google-maps gmaps4rails