使用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上的描述,我直接在视图上键入,确保它将出现在所有其他javascripts Gmaps4Rails转储到生成的html中:
<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);
}
}
那么,关于什么是错的任何想法?
提前致谢,
马
答案 0 :(得分:6)
不是在JavaScript中执行,而是可以生成所有html,包括链接是您的控制器。 注意:要正确生成链接,我将其单独设置并将其分配给变量
这就是我的locations_controller.rb包含的内容:
@json = Location.all.to_gmaps4rails do |location, marker|
location_link = view_context.link_to location.name, location_path(location)
marker.title location.name
marker.infowindow "<h4><u>#{location_link}</u></h4>
<i>#{location.address}</i>"
end
当你正确设置它时,你会产生看起来像这样的哈希
{"title":"Home",
"description":"<h4><u><a href=\"/locations/1\">Place</a></u></h4>
<i>123 E Main St. Anytown, USA</i>",
"lat":29.585019,
"lng":-81.319479}
维基页面here帮助了我。希望这可以帮助你