Ruby on rails gmaps4rails:如何在标记上实现可点击链接
使用Rails 3.2.13、ruby 1.9.3p374和Gmaps4rails 1.5.6 一开始,我认为应该是非常直截了当的:我想显示一个标记,当单击它时,它将重定向到一个特定的路径,并在同一个窗口中加载该路径 Gmaps4rails在其应用程序中没有简单的选项。所以我花了无数个小时在网上搜索,我在StackOverflow中找到了两个最好的点击:闻起来像是正确的路径,但似乎也很有用 最后,我的控制器看起来像:Ruby on rails gmaps4rails:如何在标记上实现可点击链接,ruby-on-rails,ruby,ruby-on-rails-3,google-maps,gmaps4rails,Ruby On Rails,Ruby,Ruby On Rails 3,Google Maps,Gmaps4rails,使用Rails 3.2.13、ruby 1.9.3p374和Gmaps4rails 1.5.6 一开始,我认为应该是非常直截了当的:我想显示一个标记,当单击它时,它将重定向到一个特定的路径,并在同一个窗口中加载该路径 Gmaps4rails在其应用程序中没有简单的选项。所以我花了无数个小时在网上搜索,我在StackOverflow中找到了两个最好的点击:闻起来像是正确的路径,但似乎也很有用 最后,我的控制器看起来像: @gmapsjson = current_user.houses.all.to
@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>
Gmaps.map=新的Gmaps4RailsGoogle();
Gmaps.load_map=函数(){
Gmaps.map.map\u options.auto\u zoom=false;
Gmaps.map.map_options.zoom=3;
初始化();
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();};
标记出现在正确的位置。但是现在,为了解释上面描述的内容,我直接在视图中键入,确保它将出现在生成的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>
函数重定向到(url){
window.location=url
};
Gmaps.callback=函数(){
对于(变量i=0;i
但现在,每当我加载页面时,我的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);
}
}
TypeError:Gmaps[load\u function\u name]不是函数
用于(输入GMAP){
值=GMAP[键];
searchloadinclude=key.search(/load/);
如果(searchLoadIncluded===-1){
加载\函数\名称=“加载”+键;
_push(Gmaps[load_function_name]());而不是在JavaScript中执行,您只需生成所有html,包括作为控制器的链接。
注意:为了正确生成链接,我单独设置并将其分配给变量
这是my 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
@json=Location.all.to_gmaps4rails do |位置,标记|
位置链接=查看上下文链接到位置名称,位置路径(位置)
marker.title location.name
marker.infowindow“#{location_link}
#{location.address}”
终止
正确设置后,将生成如下所示的哈希值
{"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}
{“标题”:“主页”,
“说明”:
123 E Main St.Anytown,美国“,
“lat”:29.585019,
“液化天然气”:-81.319479}
wiki页面帮助了我。希望这对你有所帮助,而不是用JavaScript,你可以生成所有的html,包括你的控制器的链接。
注意:为了正确生成链接,我单独设置并将其分配给变量
这是my 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
@json=Location.all.to_gmaps4rails do |位置,标记|
位置链接=查看上下文链接到位置名称,位置路径(位置)
marker.title location.name
marker.infowindow“#{location_link}
#{location.address}”
终止
正确设置后,将生成如下所示的哈希值
{"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}
{“标题”:“主页”,
“说明”:
123 E Main St.Anytown,美国“,
“lat”:29.585019,
“液化天然气”:-81.319479}
维基页面帮助了我。希望这对你有所帮助。你尝试过这个答案吗?我尝试过,是的。在原始问题中提到“但这个似乎也很有用”。但感谢你提醒我注意这一点。:)这种方法在javascript控制台中给了我一个未知的“谷歌”。你尝试过这个答案吗?我尝试过,是的。它被提到为在最初的问题中,“但这一个似乎也很有用”。但感谢你提醒我注意这一点。:)这种方法在javascript控制台中给了我一个未知的“谷歌”。